Get-ExpiredWebhook.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID af07f39d-34b6-4104-8121-69ebffdd9222 .AUTHOR Stefan Roth .COMPANYNAME stefanroth.net .LICENSEURI http://stefanroth.net .DESCRIPTION This runbook gets all webhooks within in your resource group and checks if the webhook will expire within the defined time range (days). The runbook will check if the expiration date is within the date from now + days. If so it will send a mail out to the address specified. The runbook uses an Automation Credential to authenticate the user sending the email. #> <# .SYNOPSIS Sends email about expiring webhooks .PARAMETER To Set the email of the recipient e.g. max@domain.com .PARAMETER From Set the email of the sending mail account e.g. admin@domain.com .PARAMETER DaysToExpiration Provide within how many days from today the webhook will expire. .PARAMETER AutomationCredentialAssetName Provide Azure Automation Asset name of the credential for accessing the Azure. .PARAMETER MailCredentialAssetName Provide Azure Automation Asset name of the credential for accessing Office 365. .EXAMPLE Get-ExpiredWebhook -To "stefan.roth@domain.ch" -From "admin@domain.net" -DaysToExpiration 200 -AutomationCredentialAssetName "Admin" -MailCredentialAssetName "Office365" #> [CmdletBinding()] Param( [Parameter(Mandatory=$True,Position=1)] [string] $To, [Parameter(Mandatory=$True,Position=2)] [string] $From, [Parameter(Mandatory=$True,Position=3)] [int] $DaysToExpiration, [Parameter(Mandatory=$True,Position=4)] [string] $AutomationCredentialAssetName, [Parameter(Mandatory=$True,Position=5)] [string] $MailCredentialAssetName ) #Update to the name of the credential asset in your Automation account # Get the credential asset with access to my Azure subscription $AzureCred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName $MailCred = Get-AutomationPSCredential -Name $MailCredentialAssetName # Authenticate to Azure Service Management and Azure Resource Manager Add-AzureAccount -Credential $AzureCred | Out-Null Add-AzureRmAccount -Credential $AzureCred | Out-Null Get-AzureRmResourceGroup | ` Get-AzureRmAutomationAccount | ` Get-AzureRmAutomationWebhook | ` ForEach($_ )` { If ((New-TimeSpan -Start (Get-Date).ToUniversalTime() -End $_.ExpiryTime.UtcDateTime).Days -lt $DaysToExpiration) { $body = @" Webhook "$($_.Name)" will expire on $($_.ExpiryTime). It is linked to $($_.ResourceGroupName)\$($_.AutomationAccountName)\$($_.RunbookName) "@ Write-Output $_ Send-MailMessage -To $To -Subject "Webhook will expire" -Body $body -UseSsl -Port 587 -SmtpServer 'smtp.office365.com' -From $From -BodyAsHtml -Credential $MailCred } } |