Powershell 上部管道中的变量不可访问
无法在管道导出csv中访问变量$Comp。输出的excel文件名类似于“”\u Users.csvPowershell 上部管道中的变量不可访问,powershell,Powershell,无法在管道导出csv中访问变量$Comp。输出的excel文件名类似于“”\u Users.csv get-content "Servers.txt" | foreach-object { $Comp = $_ if (test-connection -computername $Comp -count 1 -quiet) { } Else { Write-Warning "Server '$Comp' is Unreachable hence Co
get-content "Servers.txt" | foreach-object {
$Comp = $_
if (test-connection -computername $Comp -count 1 -quiet)
{
} Else
{
Write-Warning "Server '$Comp' is Unreachable hence Could not fetch data"
}
}|Export-Csv -NoTypeInformation "'$Comp'_Users.csv"
除了脚本块参数外,命令行参数在管道处理开始之前绑定一次 因此,在调用传递给每个对象的脚本块之前,
“$Comp”“u Users.csv”
参数中的$Comp
(我假定您的意思是“$Comp”“u Users.csv”
)会展开
- 此时,如果从未初始化
,则它的计算结果为$Comp
,这将成为可扩展字符串中的空字符串,如症状所示,或者它使用任何先前存在的$null
值$Comp
Servers.txt
中的行)导出到不同的文件,您必须将export Csv
调用移动到ForEach对象
脚本块中