Azure Function Run PowerShell Script

Well, here we will discuss How to Run a PowerShell Script from an Azure Function. Sometimes, you will get to run one PowerShell script to delete some Azure resources daily or to perform some clean-up activity daily via a PowerShell Script. In this type of Scenario, Azure Function is one of the best solutions.

For the above requirement, you can create a PowerShell Timmer Trigger Azure Function, and you can add your PowerShell Script in the run.PS1 file and then schedule it to run in a particular time interval based on your business requirements. This solution will work like a charm for the above requirements.

Create A PowerShell Timmer Trigger Azure Function

So, since we have already created A PowerShell Azure Function App above, i.e., NewPowerShellAzureFNApp, I will use the same Function App to create the PowerShell Timmer Trigger Azure Function. If you want, you can create a new PowerShell Azure Function App and then create the PowerShell Timmer Trigger Azure Function.

So let’s create the Timmer Trigger Azure Function using the following steps

1. Click on the Functions option from the left navigation and click the + Add button on the Function App page, as highlighted below.

How To Create A PowerShell Timmer Trigger Azure Function Azure Portal

2. On the Add function window, select the Development environment option as Develop in the portal and then select the Timer Trigger as the template.

Create A PowerShell Timmer Trigger Azure Function using Azure Portal

3. Then, Provide a name for your Timmer Azure Function and provide a CORN expression for the schedule option on which the Azure function is going to execute. Here, we have provided the CORN expression as 0 * /5 ****. This means that the Azure Function will execute on each 5-minute duration. Similarly, if We mention 0 * /2 ****, the Azure Function will execute on each 2-minute interval of time. You can find more details on CORN expressions now.

Azure Function Run PowerShell Script

4. After you fill in all the details above, click on the Add button to create the PowerShell Timmer Azure Function. You can now see that the Timmer trigger Azure Function is created successfully. Click on the Code + Test link from the left navigation. You can able to see the PowerShell Code for the run.ps1 file.

Develop A PowerShell Timmer Trigger Azure Function Azure Portal

Below is the complete code that is present in the run.ps1 file

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

Now, you can add the PowerShell script in this run.PS1 file based on your requirements, and then click on the save button. Then, it will execute the Azure function on the duration you mentioned as the CORN expression based on your business needs. One more thing: you can always click on the Test/Run button to test if the Timmer trigger Azure Function is working fine or not.

Conclusion

This is How you can Develop a PowerShell Timmer Trigger Azure Function and run a PowerShell script from the Azure Function.

You may also like following the articles below