2017-06-02 30 views
2

私は長い間実行されるスクリプトを書いています。とりわけ、定期的にファイルに保存されるログエントリの配列を蓄積します(エントリが追加されるたびにではありません)。私が考慮する必要がある問題は、スクリプトが途中で終了し、いくつかのログファイルが保存されないとどうなるかです。強制終了時にコードブロックを実行しますか?

実際に終了する前にPowershellにAdd-Content -Path $LogLocation -Value $NewLogsのようなコードブロックを実行するよう指示する方法はありますか?

答えて

2

Use finally

try { 
    # do stuff 
} finally { 
    Add-Content -Path $LogLocation -Value $NewLogs 
    # this gets executed if an exception is thrown, if the script is stopped, 
    # or if it exits normally; pretty much always 
} 
関連する問題