Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - an empty pipe element is not allowed

Tags:

powershell

csv

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.

like image 768
theshizy Avatar asked Oct 29 '13 07:10

theshizy


3 Answers

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
like image 97
mjolinor Avatar answered Oct 24 '22 09:10

mjolinor


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
like image 26
David Martin Avatar answered Oct 24 '22 09:10

David Martin


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.

like image 5
Saleh Rahimzadeh Avatar answered Oct 24 '22 08:10

Saleh Rahimzadeh