Skip to content

Instantly share code, notes, and snippets.

@rbenigno
Created February 8, 2017 15:19
Show Gist options
  • Save rbenigno/df7c8cee74035fe046f31c00e441b33b to your computer and use it in GitHub Desktop.
Save rbenigno/df7c8cee74035fe046f31c00e441b33b to your computer and use it in GitHub Desktop.
PowerShell function for logging in key value pairs for Splunk
# Write log entries in a format that can be easily ingested by Splunk
<# Add the following to your script, then use "Write-MyLog" to add log entries
Import-Module "D:\BATCH_JOBS\Logging.psm1"
Function Write-MyLog {
Param ([string]$logstring)
$Logfile = "D:\BATCH_JOBS\LOGS\<<JOB_NAME>>.log"
Add-LogEntry -LogPath $LogFile -JobName "<<JOB_NAME>>" -Message "$logstring"
}
#>
Function Add-LogEntry
{
Param (
[string]$LogPath,
[string]$JobName,
[string]$Message
)
# Log entry starts with a timestamp
$logstring=(Get-Date -format o)
# Add a job name if we have one
If ($JobName) {$logstring="$logstring jobname=`"$JobName`""}
Add-content $LogPath -value "$logstring message=`"$Message`""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment