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.
        It uses [string]$script:uriSplunk, [string]$script:headerSplunk, [string]$script:ScriptFileName, [switch]$script:Email, [switch]$script:Slack, [string]$script:Body.
        Those will all need to be defined in the script that calls this function.
        If email or slack are enabled, the message will be added to the $script:Body variable.
        This is useful for sending a summary email or slack message at the end of a script.
    #>

    param (
        [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
        [String]$Msg,
        [Switch]$IsError
    )

    if ($Splunk -and $AzureKeyName) {
        $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
    }
    if ($Script:Email -or $Script:Slack) {
        $Script:Body += $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
}