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?
$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:
$results | Export-csv "results.csv" -NoTypeInformation
within the foreach loop but it only returns the last record.
A foreach loop doesn't ouput to the pipeline. You can make it do that by making the loop a sub-expression:
$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 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:
$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
Some of flow-control expressions do not stream their output. But they can be assigned to variables, but no output streaming.
You can put your expression output stream in a Subexpression $()
and then pipe with another command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With