2017-10-28 15 views
1

두 개의 개별 텍스트 파일을 얻기 위해 PowerShell의 간단한 테스트 스크립트를 separate warning message from stdout/result에 작성하려고합니다. 하나는 경고 용이고 다른 하나는 표준 결과 용입니다. PowerShell Redirection입니다. ,경고 메시지를 stdout/powershell 리디렉션의 결과에서 분리하는 방법은 무엇입니까?

PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt 
pause 

하지만 내 문제는 warning.txt입니다 완전히 비어 : 나는 명령 아래 사용하여 .BAT 파일을 사용하여 output.ps1을 실행하기 위해 노력하고있어

Write-Output "This is result" 
Write-Warning "This is warning" 

가 :

은 여기 내 간단한 output.ps1 스크립트입니다 경고 및 결과 모두는 results.txt에 기록됩니다.

This is result 
WARNING: This is warning 

나는 두 개의 파일로 경고 및 오류를 분리하는 PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt에서 실종 무엇?


편집 : 표준 오류를보고 수정, 2>가 노력하고 있습니다. 내가

PowerShell.exe -Command "& '%~dp0output.ps1'" 2>error.txt 3>warning.txt >results.txt 

를 사용하여 2>error.txt를 사용하여 별도의 파일에 표준 오류 리디렉션 나는 다음과 같은 방식으로 오류가 발생하는 output.ps1을 수정하면

Write-Output "This is result" 
Write-Warning "This is warning"" 
This will generate error because this is not a ps command. 

이 같은 오류가 발생합니다 경우에도

파일로 예상 됨 error.txt with results.txt 여기서 results.txt은 경고와 stdout/result의 조합입니다. warning.txt은 여전히 ​​비어 있습니다. PowerShell 경고가 필터링되지 않고 별개의 파일로 제대로 리디렉션되지 않는 이유는 아직도 모르겠습니다.

error.txt :

this : The term 'this' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again. 
At C:\Users\Dell\Desktop\output.ps1:3 char:1 
+ This will generate error because this is not a ps command. 
+ ~~~~ 
    + CategoryInfo   : ObjectNotFound: (this:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

RESULTS.TXT :

This is result 
WARNING: This is warning 

나는 윈도우 10와 PowerShell을 버전 5.1.14393.1358을 사용하고 있습니다. 당신이을 경고 PS1 파일 자세한, 을 실행하는 powershell.exe를 사용하는 경우

+0

'2> warning.txt'은 무엇입니까? –

+0

powershell/cmd cant 리디렉션 경고 일 수 있습니까? 2> ..> 3>을 사용하여 리디렉션 할 수있는 오류는 공백 출력을 생성합니다. – user879

+1

알다시피, cmd는 오류와 표준 스트림 만 있습니다. – Clijsters

답변

1

진짜 문제는, 아래의 예에서,이고, 디버그 스트림) STDOUT에 병합 - Source.

PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt 

powershell.exe는이 한계를 회피 (--- 빈 warning.txt을 생성), 추가 중간 PS1 파일을 사용하여 상기 실시 예에 대한 해결 방법이있다. 두 개의 powershell 파일과 bat 파일을 사용하여 error/warning/results에 대해 세 개의 개별 파일을 얻을 수 있습니다.( 2>error.txt 3>warning.txt >results.txt)

아래와 같은 내용으로 output.ps1과 동일한 내용이 있습니다.

Write-Output "This is result" 
Write-Warning "This is warning"" 
This will generate error because this is not a ps command. 

다음으로 다른 powershell 파일을 만듭니다. (나는 그것을 newout.ps1라고 부를 것이다) 및 newout.ps1output.ps1 이하의 내용으로 실행할 수있다.

.\output.ps1 2>error.txt 3>warning.txt >results.txt 

마지막으로 당신의 박쥐 파일은 이러한 방식으로 newout.ps1 대신 1 output.ps1를 실행할 수 있습니다.

PowerShell.exe -Command "& '%~dp0newout.ps1'" 

원하는 결과를 제공하는 세 개의 개별 파일을 제공합니다.