PowerShell 2、3、x には、PowerShell ウィンドウのすべての出力をテキスト ファイルに記録しようとするTranscript コマンドレットがあります。いくつかの制限があります。
- 外部実行可能 stdout、stderr はキャプチャされません
これを示すコード例を次に示します。
$this_path = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$log_path = Join-Path -Path $this_path -ChildPath script_log.txt
Start-Transcript -Path $log_path
$VerbosePreference = "continue"
$ErrorActionPreference = "continue"
$DebugPreference = "continue"
$WarningPreference = "continue"
& hostname.exe
Write-Host "write-host"
Write-Verbose "write-verbose"
Write-Error "write-error"
Write-Debug "write-debug"
Write-Warning "write-warning"
Get-Date
Stop-Transcript
& notepad $log_path
hostname.exe
外部実行可能ファイルであるため、出力を除いて、上記のすべてが script_log.txt にキャプチャされます。
いくつかの回避策があります。
powershell.exe -noprofile -file script.ps1 > script.log
これは の出力を含むすべてをキャプチャしますhostname.exe
が、スクリプトの外部で行われるものです。
もう 1 つは各外部コマンド用で、ホスト API を介して出力をパイプします。
& hostname.exe | Out-Default
これはスクリプトで行われますが、シェル ウィンドウの exe からテキストの色が失われます。