2013-07-12 1 views
20

PowerShell에서 echoWrite-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 :"줄이 다른 지점에 있음을 알 수 있습니다. 왜 이런거야? echoWrite-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! 
+0

질문에 대한 답변이 아니지만 PowerShell 함수에 대한 인수를 호출 할 때 괄호 안에 넣으면 안됩니다. 그것은'ValidatePath $ filename $ user'이어야합니다 –

+0

아마도 여기에 내 질문에 대답 : http://stackoverflow.com/questions/17623712/multiple-parameters-put-into-one-variable-in-powershell 감사. – BlackHatSamurai

답변

43

echoWrite-Output의 별칭으로 Success 출력 스트림에 씁니다. 따라서 파이프 라인을 통해 출력을 처리하거나 파일로 리디렉션 할 수 있습니다. Write-Host은 콘솔에 직접 기록하므로 더 이상 출력을 리디렉션/처리 할 수 ​​없습니다.

+3

@velua 편집중인 내용을 이해하지 못할 때 편집하지 마십시오. 'echo'는'Write-Output'에 대한 별칭입니다, ** Write-Host'에 대한 ** 아닙니다. 'Write-Output'과'Write-Host'는 전혀 다른 두 가지 cmdlet입니다. –

11

echo는 Write-Output의 별칭입니다. Write-Host가 '화면'에 직접 쓰는 경우 Write-Output은 파이프 라인에 씁니다. 파이프 라인이 다른 명령으로 공급되지 않으면 끝 부분의 '화면'에서 끝납니다. 차이점은 Write-Host가 화면에 직접 쓰여지고 Write-Output이 먼저 파이프 라인을 통과하고 Write-Host 이후에 화면에 표시된다는 것입니다.

Write-Output을 사용하면 출력을 파일 또는 Write-Host가 아닌 다른 명령으로 파이프/리디렉션 할 수 있습니다. 그들은 당신이 원하는 것에 따라 사용해야합니다.

자세한 내용은 here을 참조하십시오.

관련 문제