亀の甲羅2

今日もまた朝とく起きて励まなん窓に明るきありあけの月

【powershell】ログ取得 というか ターミナルの記録(Start-Transcript、Stop-Transcript)

Unixなどでも同様のことができた。ふと思い出したのでメモ。

目次

本文

1. 記録

# 開始
Start-Transcript  -Path C:\temp\test.log -Append

# ~何かコマンド実行~

# 停止
Stop-Transcript

<実行例>

PS C:\Users> Start-Transcript -Path C:\temp\test.log
トランスクリプトが開始されました。出力ファイル: C:\temp\test.log
PS C:\Users> Get-ChildItem -Path C:\temp


    ディレクトリ: C:\temp                                                             
----                 -------------         ------ ----
d-----        2021/10/18     21:57                ISL
d-----        2021/07/10     21:54                test
-a----        2021/10/25     19:40            728 test.log


PS C:\Users> Stop-Transcript
トランスクリプトが停止されました。出力ファイル: C:\temp\test.log

<log>

**********************
Windows PowerShell トランスクリプト開始
開始時刻: 20211025194048
ユーザー名: xxxxx
RunAs ユーザー: xxxxx
構成名: 
コンピューター: xxxxx (Microsoft Windows NT 10.0.19043.0)
ホスト アプリケーション: powershell.exe
~略~
**********************
トランスクリプトが開始されました。出力ファイル: C:\temp\test.log
PS C:\Users> Get-ChildItem -Path C:\temp


    ディレクトリ: C:\temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2021/10/18     21:57                ISL
d-----        2021/07/10     21:54                test
-a----        2021/10/25     19:40            728 test.log


PS C:\Users> Stop-Transcript
**********************
Windows PowerShell トランスクリプト終了
終了時刻: 20211025194120
**********************

2. スクリプトファイル内で使う

スクリプト例>

# 開始
Start-Transcript  -Path C:\temp\test.log -Append

# ~何かコマンド実行~
$a = Get-ChildItem -Path C:\temp

$a

# 停止
Stop-Transcript

<log>

**********************
Windows PowerShell トランスクリプト開始
開始時刻: 20211025193238
ユーザー名: xxxx
RunAs ユーザー: xxxx
構成名: 
コンピューター: xxxxx (Microsoft Windows NT 10.0.19043.0)
ホスト アプリケーション: C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe
~略~
**********************
トランスクリプトが開始されました。出力ファイル: C:\temp\test.log


    ディレクトリ: C:\temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2021/10/18     21:57                ISL
-a----        2021/10/25     19:19            893 aaa.log
・・・
-a----        2021/10/25     19:32              0 test.log
**********************
Windows PowerShell トランスクリプト終了
終了時刻: 20211025193239
**********************

参考

【PowerShell】Start-Transcriptを使用したセッション記録方法 - buralog

【Strat-Transcript/Stop-Transcript】PowerShellでログを取る - 適材適所


バージョン情報

> $PSVersionTable

Name                           Value                                                                                                          
----                           -----                                                                                                          
PSVersion                      5.1.19041.1023