Powershell - 不允许使用空管元素 [英] Powershell - an empty pipe element is not allowed

查看:1152
本文介绍了Powershell - 不允许使用空管元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试将结果输出到csv文件时,我一直收到错误消息 - 不允许空的管道元素。

I keep receivingthe rollowing 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.

推荐答案

我相信你所遇到的问题是使用foreach和管道,你正在处理你的项目foreach语句,但仍然期望他们在管道上。

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.

这是一个常见错误,在 Essential PowerShell:Understanding foreach and Essential PowerShell:Understanding foreach(Addendum)

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

这篇关于Powershell - 不允许使用空管元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆