Skip to content

Instantly share code, notes, and snippets.

@MSAdministrator
Created October 4, 2016 14:36
Show Gist options
  • Save MSAdministrator/2a0828e538b5885619b1c188fb8993c5 to your computer and use it in GitHub Desktop.
Save MSAdministrator/2a0828e538b5885619b1c188fb8993c5 to your computer and use it in GitHub Desktop.
Write-LogEntry
#requires -Version 1
Function Write-LogEntry
{
param (
[string]$type,
[string]$message,
[string]$Folder,
[string]$CustomMessage
)
[bool]$CustomMessage
$mutex = New-Object -TypeName 'Threading.Mutex' -ArgumentList $false, 'MyInterprocMutex'
switch ($type)
{
'Error'
{
$mutex.waitone()
"$((Get-Date).ToString('yyyyMMddThhmmss')) [ERROR]: $message" >> "$($Folder)\log.log"
if ($CustomMessage)
{
"$((Get-Date).ToString('yyyyMMddThhmmss')) [CUSTOM MESSAGE]: $CustomMessage" >> "$($Folder)\log.log"
}
$mutex.ReleaseMutex()
}
'Info'
{
$mutex.waitone()
"$((Get-Date).ToString('yyyyMMddThhmmss')) [INFO]: $message" >> "$($Folder)\log.log"
if ($CustomMessage)
{
"$((Get-Date).ToString('yyyyMMddThhmmss')) [CUSTOM MESSAGE]: $CustomMessage" >> "$($Folder)\log.log"
}
$mutex.ReleaseMutex()
}
'Debug'
{
$mutex.waitone()
"$((Get-Date).ToString('yyyyMMddThhmmss')) [DEBUG]: $message" >> "$($Folder)\log.log"
if ($CustomMessage)
{
"$((Get-Date).ToString('yyyyMMddThhmmss')) [CUSTOM MESSAGE]: $CustomMessage" >> "$($Folder)\log.log"
}
$mutex.ReleaseMutex()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment