PowerShell에서 echo
과 Write-Host
의 차이점에 대해 혼란 스럽습니다. 나는 두 파일을 가지고있다. POC.ps1
& validatePath.ps1
. 이 파일들은 로컬 컴퓨터에 있으며, 나는 원격 컴퓨터에서 Invoke-Command
을 사용하여 실행하고 있습니다. PowerShell v3.0을 사용하고 있습니다. 있는 두 개의 파일을 여기에PowerShell에서 에코와 쓰기 호스트의 차이점은 무엇입니까?
.\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
을 변경하는 경우 :
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!
:
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 user :"줄이 다른 지점에 있음을 알 수 있습니다. 왜 이런거야? echo
은 Write-Host
과 다른 방식으로 작동하는 이유는 무엇입니까?
UPDATE :
무엇 더욱 이상한 것은 그 I는 다음과 같이 두 번 스크립트를 다시 실행하는 경우 :
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