Dot-Net
PowerShell 中的 echo 和 Write-Host 有什麼區別?
我對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這就是事情變得奇怪的地方……
驗證路徑.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!
echo是 的別名Write-Output,它寫入 Success 輸出流。這允許通過管道處理輸出或將輸出重定向到文件中。Write-Host直接寫入控制台,因此無法進一步重定向/處理輸出。