Skip to content

Instantly share code, notes, and snippets.

@marckean
Created August 20, 2018 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marckean/a1da5afd839aa6af79cb7e83aa058230 to your computer and use it in GitHub Desktop.
Save marckean/a1da5afd839aa6af79cb7e83aa058230 to your computer and use it in GitHub Desktop.
# Replace with your Workspace ID
$CustomerID = $REQ_PARAMS_LogAnalyticsCustomerID
# Replace with your Log Analytics workspace Primary Key
$SharedKey = $REQ_PARAMS_LogAnalyticsPrimaryKey
#Specify the name of the record type that we'll be creating.
$LogType = $REQ_PARAMS_LogType # To be used to search for as a custom log e.g. Type=iTunesPopCharts2_CL
# Specify a time in the format YYYY-MM-DDThh:mm:ssZ to specify a created time for the records
$TimeStampField = ''
# Telstra DEV APIs - https://dev.telstra.com
$Telstra_app_key = $REQ_PARAMS_Telstra_app_key
$Telstra_app_secret = $REQ_PARAMS_Telstra_app_secret
$tel_numbers = $REQ_PARAMS_tel_numbers -split ','
$ServiceName = $REQ_PARAMS_ServiceName
$Status = $REQ_PARAMS_Status
$message = $REQ_PARAMS_Message
# Message to send
$LAmessage = [PSCustomObject]@{
Channel = 'Azure Automation Webhook'
message = $message
}
# Function to send SMS via Telstra's SMS API
function Send-TelstraSMS ($body) {
#Get Telstra API access - https://dev.telstra.com/
$auth_string = "https://api.telstra.com/v1/oauth/token?client_id=" + $Telstra_app_key + "&client_secret=" + $Telstra_app_secret + "&grant_type=client_credentials&scope=SMS"
$auth_values = Invoke-RestMethod $auth_string
# Send SMS
foreach($tel_number in $tel_numbers){
$token = $auth_values.access_token
$sent_message = Invoke-RestMethod "https://api.telstra.com/v1/sms/messages" -ContentType "application/json" -Headers @{"Authorization"="Bearer $token"} -Method Post -Body "{`"to`":`"$tel_number`", `"body`":`"$body`"}"
$sent_message
}}
# Function to create the authorization signature.
Function New-Signature ($CustomerID, $SharedKey, $date, $contentLength, $method, $contentType, $resource)
{
$xHeaders = 'x-ms-date:' + $date
$stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
$keyBytes = [Convert]::FromBase64String($SharedKey)
$sha256 = New-Object -TypeName System.Security.Cryptography.HMACSHA256
$sha256.Key = $keyBytes
$calculatedHash = $sha256.ComputeHash($bytesToHash)
$encodedHash = [Convert]::ToBase64String($calculatedHash)
$authorization = 'SharedKey {0}:{1}' -f $CustomerID, $encodedHash
return $authorization
}
# Function to create and post the request
Function Send-OMSData($CustomerID, $SharedKey, $body, $LogType)
{
$method = 'POST'
$contentType = 'application/json'
$resource = '/api/logs'
$rfc1123date = [DateTime]::UtcNow.ToString('r')
$contentLength = $body.Length
$signature = New-Signature `
-customerId $CustomerID `
-sharedKey $SharedKey `
-date $rfc1123date `
-contentLength $contentLength `
-method $method `
-contentType $contentType `
-resource $resource
$omsuri = 'https://' + $CustomerID + '.ods.opinsights.azure.com' + $resource + '?api-version=2016-04-01'
$headers = @{
'Authorization' = $signature
'Log-Type' = $LogType
'x-ms-date' = $rfc1123date
'time-generated-field' = $TimeStampField
}
$response = Invoke-WebRequest -Uri $omsuri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing
return $response.StatusCode
}
# Send to Log Analytics workspace
$json = $LAmessage | ConvertTo-Json
Write-Output -InputObject $json
Send-OMSData -customerId $customerId -sharedKey $sharedKey -body $json -logType $logType
# Send an SMS via Telstra's SMS
Send-TelstraSMS -body $message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment