ScriptLogging.psm1
<#
.SYNOPSIS Module for logging. .DESCRIPTION Developer: Mike Polselli PSVersion: 5.0 Date: 9/14/2018 Language: PowerShell Purpose: This module is used to store the logging functions used in all scripts. .EXAMPLE Start-Log -ScriptName MyScript try { Disable-ADUser $User -ErrorAction Stop Write-Log -LogMessage "Disabled $($User.Name) and moved them to the $($DestinationOU.DistinguishedName) OU." } catch { Write-LogError -ErrorMessage "Error disabling $($User.Name) and moving them to the $($DestinationOU.DistinguishedName) OU." } Stop-Log #> ### Functions ### function Get-LogTime { # Is used to pass a current time stamp whenever output is generated. (Get-Date).ToString('yyyy-MM-dd hh:mm:ss tt') } function Start-Log { <# .SYNOPSIS Starts the log file for the script it is run in. .PARAMETER ScriptName Provides a name to the log file. .EXAMPLE Start-Log -ScriptName MyScript #> [cmdletbinding(DefaultParameterSetName = "Primary")] param ( [Parameter(ParameterSetName, Position = 0, Mandatory = $true)][string]$Global:ScriptName, [Parameter(ParameterSetName)][switch]$LocalLog ) $Date = (Get-Date).ToString('yyyy-MM-dd') $Script:Log = "\\$env:USERDOMAIN\IT\Scripts\Logs\$ScriptName`_$env:COMPUTERNAME`_$Date.txt" if ($LocalLog) { $Script:Log = "C:\IT\Logs\$ScriptName`_$Date.txt" } $TestPath = Test-Path $Log if ($TestPath -eq $false) { New-Item -Path $Log -Type File -Force | Out-Null Write-Output "$(Get-LogTime) ========== $Log did not exist. Creating. ==========" >> $Log Write-Output "$(Get-LogTime) ========== $Log created, continuing script. ==========" >> $Log Write-Output "$(Get-LogTime) ========== $ScriptName started. ==========" >> $Log } else { Write-Output "$(Get-LogTime) ========== $ScriptName started. ==========" >> $Log } } function Write-Log { <# .SYNOPSIS Used to store the information that will be passed to the log file. .PARAMETER LogMessage Information you wish to pass to the log file goes here. .PARAMETER ShowOutput If this switch is used the LogMessage information will also be printed to the console. .EXAMPLE Write-Log -LogMessage 'Disabled some users and moved them to a different OU' .EXAMPLE Write-Log -LogMessage "Disabled $($User.Name) and moved them to the $($DestinationOU.DistinguishedName) OU." .EXAMPLE Write-Log -LogMessage "Disabled $($User.Name) and moved them to the $($DestinationOU.DistinguishedName) OU." -ShowOutput 2018-12-12 04:45:59 PM - Disabled John Doe and moved them to the Disabled OU. #> [cmdletbinding(DefaultParameterSetName = "Primary")] param ( [Parameter(ParameterSetName, Position = 0, Mandatory = $true)]$LogMessage, [Parameter(ParameterSetName)][switch]$ShowOutput ) if ($ShowOutput) { "$(Get-LogTime) - $LogMessage" >> $Log Write-Output "$(Get-LogTime) - $LogMessage" } else { "$(Get-LogTime) - $LogMessage" >> $Log } } function Write-LogError { <# .SYNOPSIS Used to store the error messages that will be passed to the log file. .PARAMETER ErrorMessage Error messages that you wish to pass to the log file goes here. .PARAMETER ShowOutput If this switch is used the ErrorMessage information will also be printed to the console. .EXAMPLE Write-LogError -ErrorMessage 'Failed to disable some users and move them to a different OU' .EXAMPLE Write-LogError -ErrorMessage "Error disabling $($User.Name) and moving them to the $($DestinationOU.DistinguishedName) OU." .EXAMPLE Write-LogError -ErrorMessage "Error Disabling $($User.Name) and moving them to the $($DestinationOU.DistinguishedName) OU." -ShowOutput /-------------------------------------------------------------------------------------------\ |2018-12-12 04:48:45 PM ERROR - Error Disabling John Doe and moving them to the Disabled OU.| \-------------------------------------------------------------------------------------------/ .EXAMPLE try {Get-ChildItem C:\NotARealPath -ErrorAction Stop} catch {Write-LogError $PSItem.Exception.Message -ShowOutput} /--------------------------------------------------------------------------------------------\ |2018-12-12 04:48:45 PM ERROR - Cannot find path 'C:\NotARealPath' because it does not exist.| \--------------------------------------------------------------------------------------------/ #> [cmdletbinding(DefaultParameterSetName = "Primary")] param ( [Parameter(ParameterSetName, Position = 0, Mandatory = $true)]$ErrorMessage, [Parameter(ParameterSetName)][switch]$ShowOutput ) $Length = $ErrorMessage.Length + 31 $Dashes = ('-' * $Length) if ($ShowOutput) { Write-Output "/$Dashes\" Write-Output "|$(Get-LogTime) ERROR - $ErrorMessage|" Write-Output "\$Dashes/" "/$Dashes\" >> $Log "|$(Get-LogTime) ERROR - $ErrorMessage|" >> $Log "\$Dashes/" >> $Log } else { "/$Dashes\" >> $Log "|$(Get-LogTime) ERROR - $ErrorMessage|" >> $Log "\$Dashes/" >> $Log } } function Stop-Log { <# .SYNOPSIS Stops the log file for the script it is run in. #> Write-Output "$(Get-LogTime) ========== $ScriptName finished. ==========" >> $Log } ### Functions ### |