每当我尝试将结果输出到 csv 文件时,我都会不断收到滚动错误消息 - “不允许使用空管道元素”。知道为什么会发生这种情况吗?
$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
我试过这样做:
$results | Export-csv "results.csv" -NoTypeInformation
在 foreach 循环中,但它只返回最后一条记录。
最佳答案
foreach 循环不会输出到管道。您可以通过使循环成为子表达式来做到这一点:
$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
关于Powershell - 不允许使用空管道元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19652498/