Global.ps1

Function Send-LogetoLogReport
{
    Param(
        [parameter(Mandatory=$true)]
        $FunctionName,
        [parameter(Mandatory=$true)]
        $Exception
    )

    try
    {
        $ehName = "logs" # hub name
        $ehNameSpace = "systemarteventhubs" # namespace
        $keyname = "PowershellSend" # name of the policy
        $key = "NpdfeCBfyJPyurG9EytYKtsDVXeecXCYDZA9DsjR6zk="

        # Load the System.Web assembly to enable UrlEncode
        [Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null

        $URI = "{0}.servicebus.windows.net/{1}" -f @($ehNameSpace,$ehName)
        $encodedURI = [System.Web.HttpUtility]::UrlEncode($URI)

        # Calculate expiry value one hour ahead
        $expiry = [string](([DateTimeOffset]::Now.ToUnixTimeSeconds())+3600)

        # Create the signature
        $stringToSign = [System.Web.HttpUtility]::UrlEncode($URI) + "`n" + $expiry

        $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
        $hmacsha.key = [Text.Encoding]::ASCII.GetBytes($key)

        $signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign))
        $signature = [System.Web.HttpUtility]::UrlEncode([Convert]::ToBase64String($signature))

        # create Request Body
        $body = @{
            Source="PS_PM"
            SourcePartition=$null
            LogType="Error"
            Timestamp=[System.DateTime]::UtcNow
            Message="Function: " + $FunctionName
            Exception=$Exception.Exception.Message
        } | ConvertTo-Json -Compress

        # API headers
        #
        $headers = @{
            "Authorization"="SharedAccessSignature sr=" + $encodedURI + "&sig=" + $signature + "&se=" + $expiry + "&skn=" + $keyname;
            "Content-Type"="application/atom+xml;type=entry;charset=utf-8"; # must be this
            "Content-Length" = ("{0}" -f ($body.Length))
            }

        # execute the Azure REST API
        $method = "POST"
        $dest = 'https://' +$URI  +'/messages?timeout=60&api-version=2014-01'

        Invoke-RestMethod -Uri $dest -Method $method -Headers $headers -Body $body -Verbose
    }
    catch 
    {
        try 
        {
            # create Request Body
            $body = @{
                Source="PS_PM"
                SourcePartition=$null
                LogType="Error"
                Timestamp=[System.DateTime]::UtcNow
                Message="Unable to send the log report in " + $FunctionName
                Exception=$null
            } | ConvertTo-Json -Compress

            # API headers
            #
            $headers = @{
                "Authorization"="SharedAccessSignature sr=" + $encodedURI + "&sig=" + $signature + "&se=" + $expiry + "&skn=" + $keyname;
                "Content-Type"="application/atom+xml;type=entry;charset=utf-8"; # must be this
                "Content-Length" = ("{0}" -f ($body.Length))
                }

            # execute the Azure REST API
            $method = "POST"
            $dest = 'https://' +$URI  +'/messages?timeout=60&api-version=2014-01'

            Invoke-RestMethod -Uri $dest -Method $method -Headers $headers -Body $body -Verbose                        
        }
        catch {}
    }
}