Write-UMN.psm1
# Module: Write-UMN function Write-UMN { <# .DESCRIPTION This function will send a message to a splunk HEC endpoint .PARAMETER msg string of the message to send .PARAMETER splunkIndex integer of the splunk index to send the message to .NOTES This function requires the UMN-Common module if splunk is enabled. #> param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)] [String]$Msg, [Switch]$IsError ) if ($script:Connected -and $script:Azure) { $Script:splunkIndex += 1 #region Init if ($PSPrivateMetadata.JobId.Guid) { $JobID = $PSPrivateMetadata.JobId.Guid } else { $JobID = (New-Guid).Guid } $RbHost = $Env:COMPUTERNAME ## Splunk Prep $ScriptFileName = $script:ScriptFileName $BodySplunkBase = @{'host' = $RbHost; 'source' = $ScriptFileName; 'sourcetype' = 'HEC:AzureRunbook' } #endregion $Null = Send-SplunkHEC -uri $Script:uriSplunk -metadata $BodySplunkBase -header $Script:headerSplunk -eventData @{'jobID' = $JobID; 'msg' = $Msg; 'Index' = $Script:splunkIndex } } #write outputs to the console and stop if it's an error if ($IsError) { Write-Error $Msg -ErrorAction $ErrorActionPreference } else { Write-Warning -Message $Msg } $script:summary += $Msg + "`n" } function Send-Slack { <# .DESCRIPTION This function will send a message to a slack channel .PARAMETER Message string of the message to send .PARAMETER SlackChannel string of the slack channel to send the message to .PARAMETER Title string of the title of the message .PARAMETER URI string of the URI to send the message to .EXAMPLE Send-Slack -Title "Test Message" -Message "This is a test message" -SlackChannel "#t3-virt-info" -URI (Get-AzKeyVaultSecret -VaultName 'HEAT-Automation-KV' -Name 'virt-slack-runbook-alerts' -AsPlainText)" #> param ( [Parameter (Mandatory = $True)] [string]$Message, [Parameter (Mandatory = $True)] [string]$SlackChannel, [Parameter (Mandatory = $True)] [string]$Title, [Parameter (Mandatory = $True)] [string]$URI ) $ContentType = 'application/json' $SlackBody = @" { "text": "$Title", "channel": "$slackChannel", "attachments": [ { "text": "$Message" } ] } "@ Invoke-RestMethod -Uri $URI -Method Post -Body $SlackBody -ContentType $ContentType } function Connect-Splunk { <# .DESCRIPTION This function will connect to a splunk HEC endpoint .PARAMETER uri string of the uri to connect to .PARAMETER header string of the header to use .EXAMPLE Connect-Splunk -uri 'https://splunk.umn.edu:8088/services/collector/event' -header 'Splunk 12345678-1234-1234-1234-123456789012' #> param ( [Parameter (Mandatory = $True)] [string]$URI, [Parameter (Mandatory = $True)] [string]$Header, [Parameter (Mandatory = $True)] [string]$ScriptFileName, [switch]$Azure ) $script:UriSplunk = $URI $script:HeaderSplunk = $Header $script:ScriptFileName = $ScriptFileName $script:Azure = $Azure $script:Summary = '' $script:splunkIndex = 0 $script:Connected = $true } function Get-UMNSummary { <# .DESCRIPTION This function will return the summary of the messages written to write-umn #> return $Summary } function Clear-UMNSummary { $script:summary = '' $script:splunkIndex = 0 } $Summary = '' $UriSplunk = '' $HeaderSplunk = '' $ScriptFileName = '' $Azure = $false $splunkIndex = 0 |