We all write lengthy scripts and schedule them but we forget the crucial part of logging. Without logging, we will not have any visibility if script ran without any error.
This code will help you to enable any of your script to do logging. Copy below text or download it from below link :
# This is the folder, where you want to write logs cd $home\desktop # Convert today's date to string $date = (Get-Date).ToString('yyyy-MM-dd') # Constructing complete file name with path. $logFile = "$home\desktop\Log_" + $date + ".txt" # Path of the actual script and sending warning and error to logfile. .\yourscript.ps1 3>&1 2>&1 | Out-file -filePath $logFile
You might be wondering about 3>&1 2>&1, These are the combination of streams and redirection, which helps to you to redirect warning, error etc to a file. Please use below reference table to create the combination as you need.
|Send specified stream to a file.|
|Append specified stream to a file.|
|Redirects the specified stream to the Success stream.|
Thank you for reading, I hope it was helpful.