Get Operation Status of an Azure Async Operation

Recently I was trying to make a drive bigger on an Azure VM by simply using Set-AzureOSDisk PowerShell commandlet (and the -ResizedSizeInGB switch). The problem is that this command is asynchronous meaning that it tells you that it’s done, even if it hasn’t finished yet.

The problem with this, is that there’s no way in Azure to see if the request has finished or not. All you can do is hope that it finishes without throwing an error.

Well, I’m pleased to say that (with some hacking of some other scripts that I found online) I was able to put together a PowerShell CmdLet that you can download and use to get this information from the Azure Portal. This PowerShell Commandlet uses the Microsoft Azure API to get the information about the request.

You can think of this as a PowerShell version of the Microsoft Azure Get Operation Status API.

function Get-AzureStatus
{
[CmdletBinding()]
param (
[Parameter(ParameterSetName="ByRequestId", Position=1, Mandatory)]
[string] $RequestId,
[Parameter()]
[string] $SubscriptionId
)

if (!$SubscriptionId)
{
$SubscriptionId = (Get-AzureSubscription | ? IsDefault).SubscriptionId
}

$account = Get-AzureAccount | ? { $_.Subscriptions.Contains($SubscriptionId) -and $_.Type -eq "Certificate" }

if (!$account)
{
throw "Can't find an account for Azure subscription $SubscriptionId"
}

$certificate = ls Cert:CurrentUserMy | ? Thumbprint -eq $account.Id

if (!$certificate)
{
throw "Can't find the certificate for Azure account {0}" -f $account.Id
}

$uri = "https://management.core.windows.net/${SubscriptionId}/operations/${RequestId}"

try
{
$response = Invoke-WebRequest -Method GET -Uri $uri -Certificate $certificate -Headers @{ "x-ms-version" = "2014-06-01" } -

ErrorAction Ignore
}
catch
{
$message = ( $_.ErrorDetails.Message)
throw "{0}: {1}" -f $message.Error.Code, $message.Error.Message
}

$content = $response.Content
$content.ChildNodes
}

Now using this requires that you have a certificate setup for the subscription to allow access to the Microsoft Azure API. Thankfully Cindy Gross has a great post on how to get a certificate created and uploaded to Azure.

To summarize her post.

1. Create a certificate on your machine.
makecert -sky exchange -r -n "CN=" -pe -a sha1 -len 2048 -ss My "c:temp.cer"

2. Upload that certificate to the options section of the OLD PORTAL

3. Map the subscription to your certificate
$subID = "Your Subscription Id"
$thumbprint = "Your Cert Thumbprint (from the portal)"
$SubscriptionName = "Your Subscription"
$myCert = Get-Item cert:\CurrentUserMy$thumbprint
Set-AzureSubscription –SubscriptionName $SubscriptionName -SubscriptionId $subID -Certificate $myCert

4. Install the POwerShell CmdLet above and run it passing it your RequestId that you get from any Azure PowerShell cmdlet.

Denny

The post Get Operation Status of an Azure Async Operation appeared first on SQL Server with Mr. Denny.

Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trust DCAC with your data

Your data systems may be treading water today, but are they prepared for the next phase of your business growth?