Whenever an application is live it’s important to keep it up and running without downtime. But what if your application stops suddenly just because you forget to update the expired ClientSecrets? It is going to affect your customers and business and you will get lot of escalation emails. Being an application owner, you don't want your application to have Application Downtime.

To avoid all these let’s implement a PowerShell script with help of Azure Function to monitor the expiration of your Azure AD app client secrets and send an email to the respective users in advance before it expires.

I hope you are you are already aware of below technologies which is needed for this implementation, if not please have a look at each official documentation.

Create Azure Timer Trigger

How to change the schedule in Timer Trigger

How to change the schedule in Timer Trigger

How to enable AZ PowerShell module in Azure Functions

Azure functions by default support AZ module to perform operations in Azure Resources. Below are the steps to enable it.

Have a look at Azure PowerShell module commands to learn more about it.

How to enable AZ PowerShell module in Azure Functions

Enabling Managed Identity in Azure Functions

We would need to enable Managed identity to grant permission to our Azure Function app to access the Active Directory. Please take a look at use managed identities for App Service and Azure Functions to enable it.

How to assign Application administrator permission to Azure Managed identity

Now all the perquisites and the setup done for our implementation let’s see the actual script to do that,

PowerShell Code in Azure Function to monitor Azure AD Client Secrets

Now the next step is to send notification to the user, it can be any kind of webhook/API URL which receive your POST call from PowerShell and process the Request data to send emails. You can make this process easier when you go with Azure Logic Apps as it has default connectors for Mailing.

Now our eyes are on the expiring Client Secrets before it expires which monitors it on daily basis.

Note: If you don’t want to receive the emails or run the trigger, go to your Timer Trigger -> Overview page and Disable the Function temporarily.