Powershell/Private/Write-ToLog.ps1
#Logging function <# .Synopsis Write-ToLog writes a message to a specified log file with the current time stamp. .DESCRIPTION The Write-ToLog function is designed to add logging capability to other scripts. In addition to writing output and/or verbose you can write to a log file for later debugging. .NOTES Created by: Jason Wasser @wasserja Modified: 11/24/2015 09:30:19 AM .PARAMETER Message Message is the content that you wish to add to the log file. .PARAMETER Path The path to the log file to which you would like to write. By default the function will create the path and file if it does not exist. .PARAMETER Level Specify the criticality of the log information being written to the log (i.e. Error, Warning, Informational) .EXAMPLE Write-ToLog -Message 'Log message' Writes the message to c:\Logs\PowerShellLog.log. .EXAMPLE Write-ToLog -Message 'Restarting Server.' -Path c:\Logs\Scriptoutput.log Writes the content to the specified log file and creates the path and file specified. .EXAMPLE Write-ToLog -Message 'Folder does not exist.' -Path c:\Logs\Script.log -Level Error Writes the message to the specified log file as an error message, and writes the message to the error pipeline. .LINK https://gallery.technet.microsoft.com/scriptcenter/Write-ToLog-PowerShell-999c32d0 #> # Set a global parameter for debug logging Function Write-ToLog { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][ValidateNotNullOrEmpty()][Alias("LogContent")][string]$Message , [Parameter(Mandatory = $false)][Alias('LogPath')][string]$Path = "$(Get-WindowsDrive)\Windows\Temp\jcAdmu.log" , [Parameter(Mandatory = $false)][ValidateSet("Error", "Warn", "Info", "Verbose")][string]$Level = "Info" # Log all messages if $VerbosePreference is set to ) Begin { # Set VerbosePreference to Continue so that verbose messages are displayed. $VerbosePreference = 'Continue' } Process { # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path. If (!(Test-Path $Path)) { Write-Verbose "Creating $Path." New-Item $Path -Force -ItemType File } Else { # Nothing to see here yet. } # Format Date for our Log File $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # Write message to error, warning, or verbose pipeline and specify $LevelText if ($Script:AdminDebug) { Switch ($Level) { 'Error' { Write-Error $Message $LevelText = 'ERROR:' } 'Warn' { Write-Warning $Message $LevelText = 'WARNING:' } 'Info' { Write-Verbose $Message $LevelText = 'INFO:' } 'Verbose' { Write-Verbose $Message $LevelText = 'INFO:' } } } else { Switch ($Level) { 'Error' { Write-Error $Message $LevelText = 'ERROR:' } 'Warn' { $LevelText = 'WARNING:' } 'Info' { $LevelText = 'INFO:' } 'Verbose' { Write-Verbose $Message $LevelText = 'INFO:' } } } # Add the message to the log messages and space down $logMessage = "$FormattedDate $LevelText $Message" + "`r`n" if ($Script:ProgressBar) { Update-LogTextBlock -LogText $logMessage -ProgressBar $Script:ProgressBar } # Write log entry to $Path Add-Content -Value "$FormattedDate $LevelText $Message" -Path $Path -Encoding utf8 # "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append -Encoding utf8 } End { } } |