我對PowerShell中echo
和Write-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!
不是您的問題的答案,但PowerShell函數的參數在調用時不應放在括號內。它應該是'ValidatePath $ filename $ user' –
這可能在這裏回答我的問題:http://stackoverflow.com/questions/17623712/multiple-parameters-put-into-one-variable-in-powershell謝謝。 – BlackHatSamurai