Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$adminnumbers = @("0402345678", "0401234567")
# Get the latest event of the password change
$who = (Get-EventLog -LogName Application | where {$_.EventID -eq "656"} | select -First 1).Message | Select-String -Pattern 'CN=(.*?),' -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Groups[1].Value}
$when = (Get-EventLog -LogName Application | where {$_.EventID -eq "656"} | select -First 1).Message | Select-String -Pattern 'Change\sDate\s:\s(.*?)[\n]' -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Groups[1].Value}
if($who.count -eq 1){$message = $who + " password has been changed in O365: " + $when} else{$message = $who[0] + " password has been changed in O365: " + $when[0]}
#Get Telstra API access - https://dev.telstra.com/
$app_key = "<My_Key>"
$app_secret = "<My_Secret>"
$auth_string = "https://api.telstra.com/v1/oauth/token?client_id=" + $app_key + "&client_secret=" + $app_secret + "&grant_type=client_credentials&scope=SMS"
$auth_values = Invoke-RestMethod $auth_string
# Send SMS to admin
foreach($adminnumber in $adminnumbers){
$tel_number = $adminnumber
$token = $auth_values.access_token
$body = $message.TrimEnd()
$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
}
if($who.count -eq 1){
# Connect to on-prem AD
Import-Module ActiveDirectory
$OU = 'OU=RT Users,DC=rtdc,DC=local'
$aduserrawmob = (Get-ADuser -SearchBase $OU -Filter * -Properties mobile | where {$_.Name -eq $who}).mobile
$adusermob = $aduserrawmob.replace('+61','').replace(' ','')
# Send SMS to user
$tel_number = $adusermob
$token = $auth_values.access_token
$body = $message.TrimEnd()
$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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment