我对PowerShell中echo
和Write-Host
之间的区别感到困惑。我有两个文件,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,
[string] $user)
function ValidatePath( $filename, $user, $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!
您会注意到“这是用户:”这一行位于不同的位置。为什么是这样?为什么echo
的工作方式与Write-Host
不同?
更新:
更奇怪的是,如果我像这样两次重新运行脚本:
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
在echo
中使用validatePath.ps1
时,它会为我提供此输出:
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!
答案 0 :(得分:49)
echo
是Write-Output
的别名,它写入Success输出流。这允许通过管道处理输出或重定向到文件。 Write-Host
直接写入控制台,因此无法进一步重定向/处理输出。
答案 1 :(得分:12)
echo是Write-Output的别名。在Write-Host直接写入'screen'的地方,Write-Output写入管道。如果管道没有输入其他命令,它最终也会在“屏幕”上结束。您看到的差异是Write-Host直接写入屏幕,其中Write-Output首先通过管道,并在Write-Host之后在屏幕上结束。
使用Write-Output可以将输出管道/重定向到文件或其他命令,而Write-Host则不会。应根据您的需要使用它们。
有关详情,请参阅here。