HowTo: Perform POST call to Azure REST API using Invoke-AzureRmResourceAction PowerShell command

Not long ago, one of my colleagues asked me how we could automate an action that was not quite available in the Azure PowerShell cmdlets. Sure we can use Azure REST API, meaning acquiring a bearer token and all, but if I have the choice, I prefer to stay away from this. I'll show you how to perform POST action on resources on Azure using native Azure PowerShell v1.x commands.

A little hidden gem

There is a hidden gem in the AzureRM.Resources commands named Invoke-AzureRmResourceAction and I use it from time to time to perform POST call against Azure REST APIs for functionalities that are not yet surfaced as native commands in PowerShell.
Invoke-AzureRmResourceAction command in Azure.RM.Resources module

How to use it

First, you need to determine what action you want to perform. To obtain the list of all available action you can execute the following command:

Get-AzureRmProviderOperation -OperationSearchString * | Where { $_.Operation -like '*action' } | Format-Table  

Let's say you are after the purge action of the Microsoft.Cdn resource provider, you can always do

Get-AzureRmProviderOperation -OperationSearchString Microsoft.Cdn/* | Where { $_.Operation -like '*action' } | Format-Table  

List of available actions for Microsoft.Cdn resource provider

You need to locate the desired action, and note what is the OperationName for it.

Finally how to invoke the purge command using Invoke-AzureRmResourceAction:

Invoke-AzureRmResourceAction `  
    -ResourceGroupName $ResourceGroupName `
    -ResourceType 'Microsoft.Cdn/profiles/endpoints' `
    -ResourceName $ProfileName/$EndpointName `
    -ApiVersion '2015-06-01' `
    -Action 'Purge' `
    -Parameters @{ ContentPaths = '/*' } `

Happy automation!


I am the proud father of two little gems. A beer & wine enthusiasm. For everything else, I work and play with Azure at day, and I am an Azure MVP & Advisor at night.
Montreal, Canada