This lightweight PowerShell module is for easily creating log files for your PowerShell scripts. Logging is very important to see whats happen, with this module you don't have to care about this anymore.
This module provides the following functionality:
- Create a new log file with timestamp in the filename
- Log Warnings, Error or informational messages with time stamp to that logfile
- As an alternative you can also log your messages to Windows Event Log
There are several steps possible, follow the instructions below:
- Download the PSOHLogging Module from the Module directory in this repo
- Copy the PSLogging directory (and its contents) to your PSModulePath location. This can be either of the following locations:
- Install for Current User -
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules) - Install for All Users - `$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)``
- For more information see Microsoft TechNet - Installing PowerShell Modules
- Install for Current User -
- To use POSHLogging in your own scripts just run
Import-Module POSHLogging. - You should also be able to use the PowerShell Gallery by running
Install-Module POSHLogging.
After you import the POSHLogging module into your script you can start.
- To create the log file for your script start with
$Logfile = Start-Flog "C:\temp" "MyScript". The first parameter is the log file destination folder, if not already there, it will be created. The second parameter is the name of your script or whatever you want to name it. As the result of the function call above the directory C:\temp would be created (if not already exist) and a log file MyScriptYYYY-mm-dd_HH:MM.log will be created as well. The full path to the log file is than in the variable$LogFilefor the following function calls. - To send messages to you log file use the following function call:
Write-FLog -InformationType "I" -Text "Copy files to C:\Temp" -LogFile $LogFile. Possible information types are:- E for Error
- W for Warning
- I for Information
- S for Success
- At the end of your script just call
Stop-FLog $LogFile "MyScript"which adds a footer message with time stamp to your log file. - Sometimes it's helpful to have logging in Windows Event logs as well, e.g. special things which should be logged for monitoring systems. In this case you can call
Write-ELog -EventLog "System" -Source "MyScript" -EventID "3000" -Type "Error" -Message "An error occurred"to write an entry to the event log System with the Source "MyScript", EventID "3000" of type "Error" and the message "An error occurred".
Import-Module POSHLogging
# Start logging
$Logfile = Start-FLog "C:\logs" "MyScript"
# Log Error
write-FLog "E" "This is an error." $LogFile
Write-FLog "W" "This is a warning" $Logfile
Write-FLog "I" "This is an information" $Logfile
# Stop logging
Stop-FLog $LogFile "MyScript"
# Notify eventlog
Write-ELog -EventLog "System" -Source "MyScript" -EventID "3000" -Type "Information" -Message "Script finished succesful."
