.net PowerShell 中的 echo 和 Write-Host 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17623644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between echo and Write-Host in PowerShell?
提问by BlackHatSamurai
I'm confused about the difference between echoand Write-Hostin PowerShell. I have two files, POC.ps1& validatePath.ps1. These files are on my local machine, and I'm running them on a remote machine using Invoke-Command. I am using PowerShell v3.0.
我对PowerShellecho和Write-HostPowerShell之间的区别感到困惑。我有两个文件,POC.ps1& validatePath.ps1. 这些文件在我的本地机器上,我在远程机器上使用Invoke-Command. 我正在使用 PowerShell v3.0。
To execute both of these scripts I use the command:
要执行这两个脚本,我使用以下命令:
.\POC.ps1 -filename C:\Users -user Blaine
Here are the two files:
这是两个文件:
POC.ps1:
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
Here is where things get weird...
这就是事情变得奇怪的地方......
validatePath.ps1:
验证路径.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
}
When I run the above script, this is the output:
当我运行上面的脚本时,这是输出:
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!
But if I change the validatePath.ps1to this:
但是,如果我将其更改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
}
This is the output:
这是输出:
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!
You will notice that the line "This is the user:" is in different spots. Why is this? Why does echowork differently than Write-Host?
您会注意到“This is the user:”这一行位于不同的位置。为什么是这样?为什么echo与Write-Host?
UPDATE:
更新:
What is even more strange is that if I rerun the script twice like this:
更奇怪的是,如果我像这样重新运行脚本两次:
POC.ps1:
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
It gives me this output when using echoin validatePath.ps1:
使用echoin时它给了我这个输出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!
回答by Ansgar Wiechers
echois an alias for Write-Output, which writes to the Success output stream. This allows output to be processed through pipelines or redirected into files. Write-Hostwrites directly to the console, so the output can't be redirected/processed any further.
echo是 的别名Write-Output,它写入成功输出流。这允许通过管道处理输出或重定向到文件中。Write-Host直接写入控制台,因此无法进一步重定向/处理输出。
回答by Lars Truijens
echo is an alias for Write-Output. Where Write-Host directly writes to the 'screen', Write-Output writes to the pipeline. If the pipeline is not feed into some other command it ends on the 'screen' in the end as well. The difference you see is Write-Host getting written to the screen directly where Write-Output first goes through the pipeline and ends up on the screen after Write-Host.
echo 是写输出的别名。Write-Host 直接写入“屏幕”,Write-Output 写入管道。如果管道没有输入其他命令,它最终也会在“屏幕”上结束。您看到的不同之处在于 Write-Host 直接写入屏幕,其中 Write-Output 首先通过管道并在 Write-Host 之后最终出现在屏幕上。
Using Write-Output allows you to pipe/redirect the output to a file or to another command, where Write-Host does not. They should be used depending on what you want.
使用 Write-Output 允许您将输出通过管道/重定向到文件或另一个命令,而 Write-Host 则不然。应该根据您的需要使用它们。
See herefor more.
请参阅此处了解更多信息。

