Skip to content

Instantly share code, notes, and snippets.

@MatthewJDavis
Last active May 20, 2019 02:35
Show Gist options
  • Select an option

  • Save MatthewJDavis/d2a52cb13b4ddf53eefd9e680b6327e3 to your computer and use it in GitHub Desktop.

Select an option

Save MatthewJDavis/d2a52cb13b4ddf53eefd9e680b6327e3 to your computer and use it in GitHub Desktop.
Check AD has synced to Azure AD within the last two hours, if not, send a slack message. Azure Automation runbook.
# Azure runbook running under an automation account.
#Requires -Modules MSOnline
Import-Module -name MSOnline
$creds = Get-AutomationPSCredential -Name 'AzureADConnectSyncAccount'
Connect-MsolService -Credential $creds
$SlackHook = Get-AutomationVariable -Name 'AlertsSlackWebHookUri'
# Slack helper function
function New-MDSlackMessage {
<#
.SYNOPSIS
Send a message to Slack
.DESCRIPTION
Send a JSON payload to slack consisting of a text message
.EXAMPLE
New-MDSlackMessage -URI $SlackHook -Message 'hi'
Send the message 'hi' to slack
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
Requires a Slack app https://api.slack.com/apps and the URI of the slack hook and a valid bearer token passed to it
#>
param(
# URI of Slack Hook
[Parameter(Mandatory = $true, Position = 0)]
[string]
$Uri,
# Message To Send
[Parameter(Mandatory = $true, Position = 1)]
[string]
$Message
)
$payload = @{
"text" = $Message
}
Invoke-RestMethod -Method Post -Uri $SlackHook -Body (ConvertTo-Json -InputObject $payload -Compress) -UseBasicParsing | Out-Null
}
<#
.SYNOPSIS
Check last sync time is within 2 hours
.DESCRIPTION
Uses MSOL command to get the last sync time and check it is within two hours.
If it isn't, send an alert to the slack alerts room.
#>
$CompanyInformation = Get-MsolCompanyInformation
if ((Get-Date).AddHours(-2) -gt $CompanyInformation.LastDirSyncTime) {
# Send error message
$Message = "Azure AD Connect`nNo AD to Azure sync for over 2 hours`nLast sync time was: $($CompanyInformation.LastDirSyncTime)`nPlease check sync service on the sync server."
Write-Output $Message
New-MDSlackMessage -Uri $Uri -Message $Message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment