PowerShell에서 echo와 Write-Host의 차이점은 무엇입니까?
저는 두 사람 사이의 차이점에 대해 혼란스럽습니다.echo
그리고.Write-Host
PowerShell에 있습니다.파일이 두 개 있는데,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
여기서 일이 이상해지고...
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 user:"라는 행이 다른 위치에 있음을 알 수 있습니다.왜 이러한가?왜 그럴까요?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
, 성공 출력 스트림에 기록됩니다.이를 통해 파이프라인을 통해 출력을 처리하거나 파일로 리디렉션할 수 있습니다.Write-Host
콘솔에 직접 쓰기 때문에 출력을 재연결/processed할 수 없습니다.
echo는 Write-Output의 별칭입니다.Write-Host가 '화면'에 직접 쓰는 경우 Write-Output은 파이프라인에 씁니다.파이프라인이 다른 명령에 입력되지 않으면 결국 '화면'에서도 종료됩니다.Write-Host는 Write-Output이 먼저 파이프라인을 통과하고 Write-Host 다음에 화면에 표시되는 직접적인 차이입니다.
Write-Output을 사용하면 출력을 파일 또는 다른 명령으로 파이프/리다이렉트할 수 있습니다. Write-Host는 그렇지 않습니다.원하는 것에 따라 사용해야 합니다.
자세한 내용은 여기를 참조하십시오.
언급URL : https://stackoverflow.com/questions/17623644/what-is-the-difference-between-echo-and-write-host-in-powershell
'sourcecode' 카테고리의 다른 글
SQL Infinite Calendar Pattern (0) | 2023.10.11 |
---|---|
현재 노드 대.XSLT/XPath의 컨텍스트 노드? (0) | 2023.10.11 |
XML 문자열을 XmlElement로 변환해야 합니다. (0) | 2023.10.11 |
오픈레이어스 vs 구글맵? (0) | 2023.10.11 |
오라클 데이터베이스가 서블릿에 요청을 보낼 수 있습니까? (0) | 2023.10.11 |