Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 上部管道中的变量不可访问_Powershell - Fatal编程技术网

Powershell 上部管道中的变量不可访问

Powershell 上部管道中的变量不可访问,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

无法在管道导出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 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对象
脚本块中