SQL Server with Mr. Denny

Dec 16 2015   5:00PM GMT

Get Operation Status of an Azure Async Operation

Denny Cherry Denny Cherry Profile: Denny Cherry

Tags:
Azure
Powershell
PowerShell Scripts
SQL Server
Windows Azure

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:\CurrentUser\My | ? 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:\\CurrentUser\My\$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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: