kharukaのブログ~お金と技術とキャリア~

Edu Fin~金融×教育~若いうちからお金について学ぶってだいじ!学んだテクノロジーはみんなの財産。過去、現在、将来の人生についてのブログ

log取得。(powershell)

test.ps1

#実行するscriptがあるディレクトリパスを取得する。
$PSPath=Split-Path -parent $myinvocation.mycommand.path 

#日時を取得
$DATETIME = Get-Date -Format "yyyy-MMdd-HHmmss"

#ログファイルのパスを取得
$TestLogFile=Join-Path $PSPath "\LOG\test_log_$DATETIME.log"

#実行するscriptがあるディレクトリにLOGフォルダがなければ作成
$LogPath=Join-Path $PSPath "\LOG"
If(-not(Test-Path -path $LogPath)) 
{ 
 New-Item $LogPath -ItemType Directory 
} 

#ログ取得開始
start-transcript $TestLogFile

#メインスクリプト
Write-Host "test"

#ログ取得終了
stop-transcript

スクリプトを実行します。

PS C:\Users\Administrator> C:\work\test\test.ps1


    ディレクトリ: C:\work\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2017/02/24     18:08                LOG
トランスクリプトが開始されました。出力ファイル: C:\work\test\LOG\test_log_2017-0224-180850.log
test
トランスクリプトが停止されました。出力ファイル: C:\work\test\LOG\test_log_2017-0224-180850.log

test_log_2017-0224-180850.log

**********************
Windows PowerShell トランスクリプト開始
開始時刻: 20170224180850
ユーザー名: <host name>\Administrator
RunAs ユーザー: <host name>\Administrator
コンピューター: <host name> (Microsoft Windows NT 10.0.14393.0)
ホスト アプリケーション: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
プロセス ID: 2032
PSVersion: 5.1.14393.693
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.693
BuildVersion: 10.0.14393.693
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
トランスクリプトが開始されました。出力ファイル: C:\work\test\LOG\test_log_2017-0224-180850.log
test
**********************
Windows PowerShell トランスクリプト終了
終了時刻: 20170224180850
**********************

参考:

mtgpowershell.blogspot.com