Created
February 8, 2017 15:19
-
-
Save rbenigno/df7c8cee74035fe046f31c00e441b33b to your computer and use it in GitHub Desktop.
PowerShell function for logging in key value pairs for Splunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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