Using the Azure REST API to Pause All AS Instances in a Resource Group

Forgetting to shutdown your Azure environments and burning through a month’s worth of Azure MSDN credit if you’re lucky (and racking up a large credit card charge if you’re not) is a pretty common experience these days. Some might even call it a right-of-passage (like running a DELETE statement in PROD and forgetting the WHERE-clause). Thankfully, Rui Romano shared a great blog post showing how to use the Azure REST API to Pause/Resume an Azure AS instance and schedule it via Azure Automation.

After having another close call last week with leaving an Azure AS environment running, I decided it was time to sit down and implement Rui’s solution. After getting Rui’s solution working (which didn’t take long at all as his instructions were very accurate) I decided to make a few minor adjustments…

  • instead of a pause/resume for a single Azure AS instance, I wanted to simply pause ALL instances in a resource group
  • added a check to see if the instance was already paused before attempting to pause it again (which causes an error)

image

The only real change is adding a REST call to retrieve all the servers in a resource group and capture the response in a variable…

#region get as-servers in ResourceGroup

    #URI TO GET SERVERS
    #GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AnalysisServices/servers?api-version=2016-05-16
    $requestUri_GetServerList = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.AnalysisServices/servers ?api-version=2016-05-16"
    
    $params = @{
        ContentType = 'application/x-www-form-urlencoded'
        Headers = @{
        'authorization'="Bearer $($Token.access_token)"
        }
        Method = 'Get'
        URI = $requestUri_GetServerList
    }

    $resp = Invoke-RestMethod @params
    
#endregion 

Then we loop through the list of Azure AS instances in the response using a foreach loop…

 

foreach ( $svr in $resp.value ) 
{
    if ( $svr.type -eq "Microsoft.AnalysisServices/servers" )
    {
        $serverName = $svr.name
        Write-Output(">   Attempting to suspend: {1}" -f $serverName)

        #snip (see next code-block)            

    }
}

One other enhancement to Rui’s original script is to check to confirm the instance isn’t already paused before trying to pause it. As this is just for my personal lab/dev environment, a basic check like this works fine… however, in a client-production scenario, you’ll probably want to enhance this state check and add some re-try logic.

Here’s the complete foreach loop:

foreach ( $svr in $resp.value ) 
{
    if ( $svr.type -eq "Microsoft.AnalysisServices/servers" )
    {
        $serverName = $svr.name
        Write-Output(">   Attempting to suspend: {1}" -f $serverName)

            
        #URI TO GET STATE
        #GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AnalysisServices/servers/{serverName}?api-version=2016-05-16
        $requestUri_GetState = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.AnalysisServices/servers/$serverName ?api-version=2016-05-16"
    
        $params = @{
            ContentType = 'application/x-www-form-urlencoded'
            Headers = @{
            'authorization'="Bearer $($Token.access_token)"
            }
            Method = 'Get'
            URI = $requestUri_GetState
        }

        $resp_state = Invoke-RestMethod @params

        if ( $resp_state.properties.state -eq "Paused" )
        {
            Write-Output ">      Server is already paused."
        }
        else 
        {

            $requestUri_Cmd = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.AnalysisServices/servers/$serverName/suspend ?api-version=2016-05-16"
    
            $params = @{
                ContentType = 'application/x-www-form-urlencoded'
                Headers = @{
                'authorization'="Bearer $($Token.access_token)"
                }
                Method = 'Post'
                URI = $requestUri_Cmd
            }
    
            Invoke-RestMethod @params
    
        }
    }
}

I’m not posting the complete script, because I want you to read all of Rui’s post… and you’ll be glad you did as he also has the steps needed to setup the Azure Automation piece.

1 thought on “Using the Azure REST API to Pause All AS Instances in a Resource Group

  1. Hi Bill,

    Thank you for providing detailed code on pausing AS servers. It helped me.

    Do you also have queries for syncing scaled out instances using REST API.

    Thanks,
    Prakhar

Leave a Reply