7

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.

3
  • 1
    To perform an Export-csv one object at a time remember to add the -Append switch. This should allow you to identify which data is causing the problem. PS. why are you using a select on a new PSObject: that's just wasting cycles.
    – Richard
    Oct 29, 2013 at 8:03
  • @Richard Thanks for the reply. Can you show me how you would go about doing this? Also why am I wasting cycles - is there a better way of doing this. Cheers
    – theshizy
    Oct 29, 2013 at 8:32
  • Re. my PS: just use the result of New-Object PSObject …, there is no need to perform the Select (it just creates a new PSObject with the named properties... which is repeating what the New-Object just did).
    – Richard
    Oct 29, 2013 at 11:08

4 Answers 4

18

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
8

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
5

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.

0

You can't pipe from ANY powershell statements. I'm not sure why.

if .. else
Switch
Do .. while
ForEach
For
While

EDIT: Another way. You can stream from a call operator and a scriptblock (or a function). Put the foreach or any other statement inside it. You won't have to wait until it finishes.

& { foreach ($i in 1..3) { $i } } | measure

Count             : 3
Average           : 
Sum               : 
Maximum           : 
Minimum           : 
StandardDeviation : 
Property          : 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.