我对PowerShell中回声和写入主机之间的区别感到困惑。我有两个文件,POC.ps1& validatePath.ps1。这些文件在我的本地机器上,我使用Invoke-Command在远程机器上运行它们。我正在使用PowerShell v3.0。
要执行这两个脚本,我使用命令:
.\POC.ps1 -filename C:\Users -user Blaine
以下是两个文件:
POC.ps1:
param($filename,$user)
echo $filename
echo "This"
echo $user
$responSEObject = Invoke-Command testcomputer -FilePath .\validatePath.ps1 -ArgumentList($filename,$user) -AsJob
while($responSEObject.State -ne "Completed")
{
}
$result = Receive-Job -Id $responSEObject.Id -Keep
echo $result
这里是什么东西变得奇怪?
validatePath.ps1:
Param([string] $filename,[string] $user)
function ValidatePath( $filename,$user,$fileType = "container" )
{
Write-Host "This is the file name: $filename"
Write-Host "This is user: $user" <--- Notice I'm using Write-Host here
$fileExist = $null
if( -not (test-path $filename -PathType $fileType) )
{
throw "$user,the path $filename does not exist!"
}
else
{
Write-Host "This is the second part"
echo $filename found!
}
Write-Host "This is the third part"
return $fileExist
}
try
{
ValidatePath($filename,$user)
}
catch
{
$e = $_.Exception
echo $e
}
当我运行上面的脚本,这是输出:
C:\Users This Blaine This is the file name: C:\Users Blaine This is user: <--- Notice where this line is? This is the second part This is the third part C:\Users Blaine found!
但是如果我将validatePath.ps1更改为:
Param([string] $filename,$fileType = "container" )
{
Write-Host "This is the file name: $filename"
echo "This is user: $user" <---notice I'm using Echo here
$fileExist = $null
if( -not (test-path $filename -PathType $fileType) )
{
throw "$user,the path $filename does not exist!"
}
else
{
Write-Host "This is the second part"
echo $filename found!
}
Write-Host "This is the third part"
return $fileExist
}
try
{
ValidatePath($filename,$user)
}
catch
{
$e = $_.Exception
echo $e
}
这是输出:
C:\Users This Blaine This is the file name: C:\Users Blaine This is the second part This is the third part This is user: <---- Notice where this line is Now? C:\Users Blaine found!
你会注意到,这行“这是用户:”在不同的地方。为什么是这样?为什么回声工作与写入主机不同?
更新:
更奇怪的是,如果我像这样重新运行脚本:
POC.ps1:
param($filename,$user)
echo $filename
echo "This"
echo $user
$responSEObject = Invoke-Command CAPTESTPK01 -FilePath .\validatePath.ps1 -ArgumentList $filename,$user -AsJob
while($responSEObject.State -ne "Completed")
{
}
$result = Receive-Job -Id $responSEObject.Id -Keep
echo $result
$filename = "C:\saddfasdfj"
#Here I run the command again,using a different file name
$responSEObject = Invoke-Command CAPTESTPK01 -FilePath .\validatePath.ps1 -ArgumentList $filename,$user -AsJob
while($responSEObject.State -ne "Completed")
{
if($responSEObject.State -eq "Failed")
{
echo "Failed"
$result = Receive-Job -Id $responSEObject.Id -Keep
echo $result
break
}
}
$result = Receive-Job -Id $responSEObject.Id -Keep
echo $resul
当在validatePath.ps1中使用echo时,它给我这个输出:
C:\Users This Blaine This is the file name: C:\Users This is the second part This is the third part This is user: Blaine <---- This line is here C:\Users found! This is the file name: C:\saddfasdfj This is user: Blaine <---- But Now it's here,where it should be? Wth? Blaine,the path C:\saddfasdfj does not exist!
echo是Write-Output的别名,它写入Success输出流。这允许输出通过管道处理或重定向到文件。写入主机直接写入控制台,因此输出无法进一步重定向/处理。