Link to home
Start Free TrialLog in
Avatar of erwin_miranda
erwin_miranda

asked on

An empty pipe element is not allowed.

$machines=Get-Content "D:\Scripts\McAfeeVersion\ComputerList.txt"   

foreach ($machine in $machines)
{
     if(Test-Connection $machine -q -count 1)
     {

          Write-Host "Querying $machine for software" -fore green
          Get-WmiObject win32_product -filter 'Name like "McAfee Agent%"' -computer $machine |
           
          Select-Object __SERVER, Name, Version

     }else

     {

          Write-Host "No response from $machine" -fore red
     }

     
} | Export-Csv  "D:\Scripts\McAfeeVersion\output_data.txt"  -NoTypeInformation

Open in new window


The code runs but there in no output or appending.

The line that is giving me problems is } | Export-Csv
Avatar of Qlemo
Qlemo
Flag of Germany image

The foreach statement is not expected to deliver any output to a pipe.  That is different from using foreach-object, which is deigned to consume and provide pipeline data.
You can either switch to using the latter:
$machines | % {
  $machine = $_
  # remainder of code
} | Export-CSV ...

Open in new window

or you make the statement part of a subexpression:
$(foreach ($machine in $machines)
{
# ...
})| Export-Csv ...

Open in new window

Avatar of erwin_miranda
erwin_miranda

ASKER

So you are saying my new ps1 will look like this

$machines=Get-Content "D:\Scripts\McAfeeVersion\ComputerList.txt"
$ (foreach ($machine in $machines){
     if(Test-Connection $machine -q -count 1){
          Write-Host "Querying $machine for software" -fore green
          Get-WmiObject win32_product -filter 'Name like "McAfee Agent%"' -computer $machine |
            Select-Object __SERVER, Name, Version
     }else{
          Write-Host "No response from $machine" -fore red
     }
} | Export-Csv D:\Scripts\McAfeeVersion\output_data.txt -NoTypeInfo
Missing closing ')' in expression.
Unexpected token 'in' in expression or statement
I get this error in ($machine in $machines)
$machines=Get-Content "D:\Scripts\McAfeeVersion\ComputerList.txt"
$(foreach ($machine in $machines){
     if(Test-Connection $machine -q -count 1){
          Write-Host "Querying $machine for software" -fore green
          Get-WmiObject win32_product -filter 'Name like "McAfee Agent%"' -computer $machine | 
            Select-Object __SERVER, Name, Version
     }else{
          Write-Host "No response from $machine" -fore red
     }
}) | Export-Csv D:\Scripts\McAfeeVersion\output_data.txt -NoTypeInfo

Open in new window

Okay...cool it woks what about the no response from the ones that are not there...this was the output. on the powershell console

Querying Computer1 for software
Querying Computer2 for software
Querying Computer3 for software
Querying Computer4 for software
No response from Computer5
No response from Computer6
No response from Computer7

and this was the output on the txt

"__SERVER","Name","Version"
"Computer1","McAfee Agent","4.8.2003"
"Computer2","McAfee Agent","4.8.2003"
"Computer3","McAfee Agent","4.8.2003"
"Computer4","McAfee Agent","4.8.2003"

as you can see there are no response from Computer5,6,7 on the output txt
Computers 5,6 and 7 could not be connected since they might be offline. Make sure they are powered ON and are on network.
okay
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are the man Qlemo...:-)
$machines=Get-Content "D:\Scripts\McAfeeVersion\ComputerList.txt"
$(foreach ($machine in $machines){
     if(Test-Connection $machine -q -count 1){
          Write-Host "Querying $machine for software" -fore green
          Get-WmiObject win32_product -filter 'Name like "McAfee Agent%"' -computer $machine |
            Select-Object _SERVER, Name, Version
     }else{
          Write-Host "No response from $machine" -fore red
          New-Object PsObject -Property @{ "_Server" = $machine; Name = ""; Version = "" }
     }
}) | Export-Csv D:\Scripts\McAfeeVersion\output_data.txt -NoTypeInfo

there is an issue...the green information is not displaying the machine name only the red
,"McAfee Agent","4.8.2003"
,"McAfee Agent","4.8.2003"
,"McAfee Agent","4.8.2003"
"N007S07HC","",""
The property containing the machine name is __Server, not _Server. My code is correctly using two underscores.
oh okay...just saying cause I copied verbatim from code you posted and after I run it again and then again..it stops writing the green output...but I will take your word sir, thank you very much for helping me.
v.r
E
You can see that yourself. Your code in http:#a40821629 differs from my code in http:#a40815867 - and I checked the question history to make sure I did not edit the comment after posting it.
Thank you sir....