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.

Update

2019-09-24: You can use Invoke-AzResourceAction on Az Azure PowerShell Module.
2019-09-24: Fixed Cdn purge code @{ ContentPaths = @('/*') }. Thanks
Roland Le Franc for reporting the error.

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.

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

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 '2017-10-12' `
    -Action 'Purge' `
    -Parameters @{ ContentPaths = @('/*') } `
    -Force

Happy automation!

Reference