Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Function to write to the log file
function Write-Log
{
<#
.SYNOPSIS
Function to write formatted information to the log file.
.DESCRIPTION
Function takes provided message and severity level (1,2,3), formats the message and writes it to the log file. The message parameter is required.
If a severity level other than 1 is needed, specify it as the second parameter.
Default logging output level can be defined at the script level using the $LogLevel variable. Options are:
1: Info
2: Warning (Default)
3: Error
.NOTES
The following variable need to be set for this function to work correctly:
$date = (Get-Date -Format "M-d-yyyy")
$logFile = "<Full path to log file>"
.EXAMPLE
Write-Log -Message "<message>" -Severity "<severity level: 1,2,3>"
Write-Log -Message "Hello World" -Severity 3
#>
param (
[Parameter(Mandatory)]
[string]$Message,
[Parameter()]
[ValidateSet('1','2','3')]
[int]$Severity = 1 # Default to a low severity. Otherwise, override
)
# Functions
function readableErrorLevel($Severity)
{
# Convert Severity level to human readable
if ( $Severity -eq 1 )
{
return "Info"
}
elseif ( $Severity -eq 2 )
{
return "Warning"
}
elseif ( $Severity -eq 3 )
{
return "Error"
}
}
# Check if log file exists. If no, create it
if ( !(Test-Path -Path $logFile) )
{
# Create log file
write-host "Creating log file: $scriptPath\$fileBase\gpuDomainJoin-$(Get-Date -Format "MM-dd-yyyy").log`n"
New-Item $logFile -type file > $null
}
# Set time for log entry
$time = (Get-Date -Format "HH:mm:ss")
if ( $LogLevel -eq 3 -and $Severity -eq 3 )
{
# Convert error level to human readable
[string]$Severity = readableErrorLevel $Severity
# Add formatted message to log file
Add-Content -Path $logFile -Value "$date $time : $Severity : $Message"
}
elseif ( $LogLevel -eq 2 -and $Severity -ge 2 )
{
# Convert error level to human readable
[string]$Severity = readableErrorLevel $Severity
# Add formatted message to log file
Add-Content -Path $logFile -Value "$date $time : $Severity : $Message"
}
elseif ( $LogLevel -eq 1 -and $Severity -ge 1 )
{
# Convert error level to human readable
[string]$Severity = readableErrorLevel $Severity
# Add formatted message to log file
Add-Content -Path $logFile -Value "$date $time : $Severity : $Message"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment