Question:
I keep receiving the following error message – “An empty pipe element is not allowed” whenever I try to pipe out my results to a csv file. Any idea why this might be happening?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$apps = Import-CSV apps.csv $computers = Import-CSV compobj.csv foreach($computer in $computers) { $computerLob = $computer.lob $lobApps = $apps | ? {$_.lob -eq $computerLob} foreach($app in $lobApps){ $appLocation = $app.location $installed=Test-Path "\\$computerHostname\$appLocation" $computerHostname = $computer.hostname $results = new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} | select Computer,App,Installed $results } } | Export-csv "results.csv" -NoTypeInformation |
I’ve tried doing this:
1 2 |
$results | Export-csv "results.csv" -NoTypeInformation |
within the foreach loop but it only returns the last record.
Answer:
I believe the problem you are having is around the use of foreach and the the pipeline, you are processing your items in the foreach statement but still expecting them to be on the pipeline.
This is a common error and is explained in more detail in the article Essential PowerShell: Understanding foreach and Essential PowerShell: Understanding foreach (Addendum).
You should be able to achieve what you want like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$apps = Import-CSV apps.csv $computers = Import-CSV compobj.csv $computers | foreach { $computer = $_ $computerLob = $computer.lob $lobApps = $apps | ? {$_.lob -eq $computerLob} $lobApps | foreach { $app = $_ $appLocation = $app.location $installed=Test-Path "\\$computerHostname\$appLocation" $computerHostname = $computer.hostname new-object psobject -property @{Computer=$computer.hostname;App=$app.appname;Installed=$installed} } } | Export-csv "results.csv" -NoTypeInformation |