Last active
August 26, 2018 00:43
-
-
Save marckean/2f951413bfdfaa2501f6873f236a9015 to your computer and use it in GitHub Desktop.
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
# 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 | |
$message = $REQ_PARAMS_Message | |
# Message to send | |
$LAmessage = [PSCustomObject]@{ | |
Channel = 'Azure Automation Webhook' | |
message = $message | |
} | |
function Send-TelstraSMS ($tel_numbers, $body) { | |
################# Get Telstra API access - https://dev.telstra.com/ | |
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$UriToken = "https://tapi.telstra.com/v2/oauth/token" | |
$body = @{ | |
client_id = $Telstra_app_key | |
client_secret = $Telstra_app_secret | |
grant_type = 'client_credentials' | |
scope = 'NSMS' | |
} | |
$contentType = 'application/x-www-form-urlencoded' | |
$auth_values = Invoke-RestMethod -Uri $UriToken -body $body -ContentType $contentType -Method Post | |
################# Provisioning - a 30 day mobile number | |
$token = $auth_values.access_token | |
$UriProvisioning = "https://tapi.telstra.com/v2/messages/provisioning/subscriptions" | |
$headers = @{ | |
'Authorization' = "Bearer $token" | |
'cache-control' = 'no-cache' | |
} | |
$JSON = @{ | |
activeDays = 30 | |
notifyURL = 'http://example.com/callback' | |
callbackData = @{"anything" = "some data"} | |
} | ConvertTo-Json | |
$JSON | |
$contentType = 'application/json' | |
$MobileNumberRaw = Invoke-RestMethod -Uri $UriProvisioning -ContentType $contentType -Headers $headers -Method Post -body $JSON | |
################# Send SMS | |
$MobileNumber = $MobileNumberRaw.destinationAddress | |
$token = $auth_values.access_token | |
$UriSend = "https://tapi.telstra.com/v2/messages/sms" | |
$JSON = @{ | |
to = @($tel_numbers) | |
body = $message | |
} | ConvertTo-Json | |
$JSON = $JSON -replace '\\u0027', '"' -replace '""', '"' | |
$JSON | |
$headers = @{ | |
'Authorization' = "Bearer $token" | |
'cache-control' = 'no-cache' | |
} | |
$contentType = 'application/json' | |
$sent_message = Invoke-RestMethod -Uri $UriSend -ContentType $contentType -Headers $headers -Method Post -Body $JSON | |
########################################################### | |
} | |
# 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-TelstraSMS -tel_numbers $tel_numbers -body $message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment