2013-07-12 97 views
20

我對PowerShell中echoWrite-Host之間的區別感到困惑。我有兩個文件,POC.ps1 & validatePath.ps1。這些文件在本地機器上,我使用Invoke-Command在遠程機器上運行它們。我正在使用PowerShell v3.0。PowerShell中的echo和Write-Host有什麼區別?

要執行這兩個腳本我用命令:

.\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! 

您會注意到「This is the user:」這一行出現在不同的位置。爲什麼是這樣?爲什麼echo的工作方式與Write-Host不同?

UPDATE:

什麼是更奇怪的是,如果我重新運行該腳本兩次這樣的:

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

不是您的問題的答案,但PowerShell函數的參數在調用時不應放在括號內。它應該是'ValidatePath $ filename $ user' –

+0

這可能在這裏回答我的問題:http://stackoverflow.com/questions/17623712/multiple-parameters-put-into-one-variable-in-powershell謝謝。 – BlackHatSamurai

回答

43

echoWrite-Output的別名,它寫入成功輸出流。這允許通過管道處理輸出或重定向到文件。 Write-Host直接寫入控制檯,因此輸出無法進一步重定向/處理。

+3

@velua請不要編輯的東西,當你甚至不明白你正在編輯。 'echo'是'Write-Output'的別名,**不是'Write-Host'的**。 'Write-Output'和'Write-Host'是兩個完全不同的小命令,它們完全不同。 –

11

echo是寫入輸出的別名。在寫主機直接寫入'屏幕'的地方,寫輸出寫入流水線。如果管道沒有送入其他命令,它最終也會在'屏幕'上結束。您看到的差異是寫入主機直接寫入輸出寫入輸出首先通過管道,並在寫入主機後結束在屏幕上。

使用寫入輸出可讓您將輸出重定向到文件或其他命令,其中寫入主機不會。他們應該使用取決於你想要什麼。

有關更多信息,請參閱here