问题描述
每当我尝试将结果输出到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.
这是一个常见错误,在 and 。
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 - 不允许使用空管元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!