verbose, ostrzegania i debug strumienie są połączone w STDOUT podczas uruchamiania skryptów PowerShell poprzez
powershell -File "C:\myscript.ps1"
więc nie możesz już więcej ich przekierowywać. Tylko strumień błędów jest inny, ponieważ wydaje się, że trafia on zarówno do STDOUT, jak i STDERR, gdzie może zostać przekierowany przez 1>
, a także 2>
.
Demonstracja:
C:\>type test.ps1
$DebugPreference = "Continue"
$VerbosePreference = "Continue"
Write-Output "Output message"
Write-Error "Error message"
Write-Verbose "Verbose message"
Write-Warning "Warning message"
Write-Debug "Debug message"
C:\>powershell -File .\test.ps1
Output message
C:\test.ps1 : Error message
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 2>nul 3>nul 4>nul 5>nul
Output message
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 1>nul
C:\>_
Jeśli chcesz przekierować strumień gadatliwy, ostrzeżenie, lub debugowania osobno należy użyć -Command
zamiast -File
i zrobić przekierowanie w PowerShell:
C:\>powershell -Command ".\test.ps1 2>$null 3>$null 5>$null"
Output message
VERBOSE: Verbose message
jednak podczas gdy w CMD możesz przekierować dowolny uchwyt do dowolnego innego uchwytu (3>&2
, 1>&5
, ...), PowerShell redirection obsługuje tylko przekierowanie do pliku (3>C:\out.txt
) lub wyjściowy strumień powodzenia (3>&1
). Próbuje przekierować do innego strumienia wygeneruje błąd:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>&2"
At line:1 char:22
+ .\test.ps1 2>out.txt 3>&2
+ ~~~~
The '3>&2' operator is reserved for future use.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
jak będzie przekierowanie różnych strumieni do tego samego pliku:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>>out.txt"
out-file : The process cannot access the file 'C:\out.txt' because it is being
used by another process.
At line:1 char:1
+ .\test.ps1 2>out.txt 3>>out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], IOException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Jeśli scalanie wyjście ostrzegania i sukces jest opcja dla Ciebie, można zrobić coś takiego:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>error.log"
lub tak:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>&1"
lub (przekierowanie wszystkich strumieni) tak:
powershell -Command ".\test.ps1 *>out.txt"
przeciwnym razie jedyną opcją widzę jest przekierowanie do różnych plikach:
powershell -Command ".\test.ps1 3>warning.log 2>error.log"