PowerShell – General Logging Script
Working in enterprise environments always requires a confidential log for scripting actions implemented in productive stages and even for testing it could be a great advantage. PowerShell offers a great possibility to log every action executed by a script. The CMDlets Start-Transcript & Stop-Transcript will help you to achieve a successful logging script.
This will be just an example how you can easyly create a logging script. There are thousands of different ways to solve this. The Microsoft documentation can be found in the following link https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1
The following script will write a general log file to C:\temp\logging.log containing all relevant basis information about the script execution.
Start-Transcript -path "C:\temp\logging.log" -Force <# your CODE here #> Stop-Transcript
Result
********************** Windows PowerShell transcript start Start time: 20171001150542 Username: DESKTOP-CA91P3Q\railm RunAs User: DESKTOP-CA91P3Q\railm Machine: DESKTOP-CA91P3Q (Microsoft Windows NT 10.0.15063.0) Host Application: C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe Process ID: 9208 PSVersion: 5.1.15063.608 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.15063.608 BuildVersion: 10.0.15063.608 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\temp\logging.log ********************** Windows PowerShell transcript end End time: 20171001150542 **********************
As you can see with only two lines of code you can achieve a good basis for logging.
Adding Timestamp
Let us extend the script that in the end we are able to produce a more detailed logging suitable for enterprise. When executing the script you know when the script was started and when its ended but in some case you need to know when every single code line was executed by your script. Therefore lets implement the function Write-Log($string)
Start-Transcript -path "C:\temp\logging.log" -Force function Write-Log($string) { $dateTimeNow = Get-Date -Format "dd.MM.yyyy - HH:mm:ss" $outStr = "" + $dateTimeNow +" "+$string Write-Output $outStr } Write-Log "------------ Start Script ------------" Write-Log "" Write-Log "Use this function to comment your script" Write-Log "" Write-Log "------------- End Script -------------" <# your CODE here #> Stop-Transcript
Result
... (removed the above lines) PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\temp\logging.log 01.10.2017 - 15:06:22 ------------ Start Script ------------ 01.10.2017 - 15:06:22 01.10.2017 - 15:06:22 Use this function to comment your script 01.10.2017 - 15:06:22 01.10.2017 - 15:06:22 ------------- End Script ------------- ********************** Windows PowerShell transcript end End time: 20171001150622 **********************
The logging is indeed easier to read also by technicians that are not well versed in programming or scripting. But when something went wrong the log file doesn’t display were we can find the executed script file to reproduce the error.
Adding Script Location
In large environments you never know if the file was executed from a fileshare or from a local drive. Lets fix that by implemnting the function function Get-ScriptName
Start-Transcript -path "C:\temp\logging.log" -Force function Write-Log($string) { $dateTimeNow = Get-Date -Format "dd.MM.yyyy - HH:mm:ss" $outStr = "" + $dateTimeNow +" "+$string Write-Output $outStr } function Get-ScriptName { if ($hostinvocation -ne $null) { $hostinvocation.MyCommand.Path } else { $script:MyInvocation.MyCommand.Path } } [string]$ScriptName = Get-ScriptName Write-Log "------------ Start Script ------------" Write-Log "" Write-Log $ScriptName Write-Log "" Write-Log "Use this function to comment your script" Write-Log "" Write-Log "------------- End Script -------------" <# your CODE here #> Stop-Transcript
Result
... (removed the above lines) PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** 01.10.2017 - 15:09:27 ------------ Start Script ------------ 01.10.2017 - 15:09:27 01.10.2017 - 15:09:27 C:\Users\<removed>\Desktop\Unbenannt1.ps1 01.10.2017 - 15:09:27 01.10.2017 - 15:09:27 Use this function to comment your script 01.10.2017 - 15:09:27 01.10.2017 - 15:09:27 ------------- End Script ------------- ********************** Windows PowerShell transcript end End time: 20171001150927 **********************
As you can see I’ve executed the file from the desktop. Don’t do that in productive environments. For testing only please 😉