Monitor Your Expired Azure App Certificates
Author: Eitan Talmi
Published on: Jan 9, 2023
This article will explain how to build an automation account to monitor your Azure App Certificates and to notify you before expiration.
Prepare the Certificates
- Open PowerShell window as administrator.
- Run the following command:
New-SelfSignedCertificate -Subject 'CN=AzureAppCertsChecker' -KeyLength 2048 -KeyUsageProperty All -KeyAlgorithm 'RSA' -HashAlgorithm 'SHA256' -Provider 'Microsoft Enhanced RSA and AES Cryptographic Provider' -NotAfter (Get-Date).AddYears(10)
- Extract the certificate from your computer using MMC.
- The certificate will be in the personal store; export it twice, one with the private key and one without.

Create the App
- Go to Azure App registrations.
- Click on New registration, give a name to the application, and click on Register.
- Click on the app you just created and click on Certificates & secrets.
- Click on Certificates and then on Upload certificate.
- Choose the certificate without the private key you exported.
- Click on API permissions.
- Click on Add a permission.
- Click on Microsoft Graph and on Application permissions.
- Search for mail and under the Mail section check all five.
- Search for application and check Application.Read.All.
- Click on Add permissions.
- Click on Grant admin consent for….
- Click on Overview and copy the Application (client) ID and Directory (tenant) ID.
Assign Roles to the App
- Go to Azure Roles.
- Click on Application Administrator.
- Click on Add assignments and add the app you created.
Create the Automation Account
- Go to Azure Automation Accounts.
- Click on Create and create a new account.
- Go to Microsoft.Graph.Authentication.
- Click on Azure Automation and then click on Deploy to Azure Automation to deploy it to your new account.
- Repeat for Microsoft.Graph.Mail, Microsoft.Graph.Users.Actions, and AzureAD.
- Go back to your Automation Account, click on Modules, and ensure the status of all modules is available.
- Click on Certificates, then on Add a certificate, and upload the certificate with the private key. Note the name you gave it.
Deploy the Code
- Click on Runbooks and then on Create a runbook.
- Type a name, select PowerShell as the runbook type, and select 5.1 as the runtime version.
- Click on Edit and paste the following code:
$cert = Get-AutomationCertificate -Name 'Certificate Name'
$TenantID = 'Tenant ID'
$ClientID = 'Client ID'
Connect-AzureAD -TenantId $TenantID -ApplicationId $ClientID -CertificateThumbprint $cert.Thumbprint
Connect-MgGraph -TenantId $TenantID -ClientID $ClientID -CertificateThumbprint $cert.Thumbprint
# Secret expiration date filter (for example 30 days)
$LimitExpirationDays = 30
# Retrieving the list of secrets that expire in the above range of days
$SecretsToExpire = Get-AzureADApplication -All:$true | ForEach-Object {
$app = $_
@(
Get-AzureADApplicationPasswordCredential -ObjectId $_.ObjectId
Get-AzureADApplicationKeyCredential -ObjectId $_.ObjectId
) | Where-Object {
$_.EndDate -lt (Get-Date).AddDays($LimitExpirationDays)
} | ForEach-Object {
$id = "Not set"
if ($_.CustomKeyIdentifier) {
$id = [System.Text.Encoding]::UTF8.GetString($_.CustomKeyIdentifier)
}
[PSCustomObject] @ {
App = $app.DisplayName
ObjectID = $app.ObjectId
EndDate = $_.EndDate
}
}
}
if ($SecretsToExpire.Count -ne 0) {
$Header = @"
"@
$results = [PSCustomObject]$SecretsToExpire | Sort-Object App | ConvertTo-Html -Fragment -As Table
$body = $results -join ' '
$HTML = -join($Header, $body)
$MessageBody = @{
ContentType = 'html'
Content = $HTML
}
write-host $MessageBody
$recipient = @(@{emailAddress = @{address = '[email protected]'}})
$NewMessage = New-MgUserMessage -UserId '[email protected]' -body $MessageBody -ToRecipients $recipient -Subject 'Expiring Client Certificate Secrets'
Send-MgUserMessage -UserId '[email protected]' -MessageId $NewMessage.id
}
Saving the Runbook
- Replace the placeholder values with your actual certificate name, tenant ID, and client ID.
- Click on Save.
- Click on Test pane, then on Start, and ensure your code runs without errors.
- Go back to your code and click on Publish.
Add a Schedule
- Click on Runbooks and then on the runbook you created.
- Click on Link to schedule.
- Click on Link a schedule to your runbook.
- Click on Add a schedule, fill up the details, and click on Create.
Limit the Application
- Create a Mail-Enabled security group.
- In this group, add the members which the app should send email to.
- From PowerShell, connect to Exchange Online.
- Run the following command:
New-ApplicationAccessPolicy -AppId <ClientID> -PolicyScopeGroupId <MailEnabledSecurityGroup> -AccessRight RestrictAccess -Description "Limit my app application emails sending"
Following these steps will help you effectively monitor your Azure App Certificates and receive notifications before they expire.
×