Skip to content

Instantly share code, notes, and snippets.

@stuartleeks
Created November 11, 2015 14:04
Show Gist options
  • Save stuartleeks/dc529eb723a3aa8ed258 to your computer and use it in GitHub Desktop.
Save stuartleeks/dc529eb723a3aa8ed258 to your computer and use it in GitHub Desktop.
ARM deployment operation utils
# gets information for non-succeeded deployment operations for the last deployment for the resource group specified in $resourceGroupName
Get-AzureResourceGroupDeployment -ResourceGroupName $resourceGroupName | sort -Descending -Property Timestamp | select -First 1 | Get-AzureResourceGroupDeploymentOperation | ?{ $_.Properties.ProvisioningState -ne "Succeeded" } | select -ExpandProperty Properties | ConvertTo-Json
# gets a summary of the deployment operations for the last deployment for the resource group specified in $resourceGroupName
Get-AzureResourceGroupDeployment -ResourceGroupName $resourceGroupName | sort -Descending -Property Timestamp | select -First 1 | Get-AzureResourceGroupDeploymentOperation | Parse-DeploymentOperationSummary
# Currently this is just a script (should probably be wrapped as a module!)
# so dot source it to make the functions available:
# . .\OperationHelpers.ps1
function ParseOperationDuration($durationString){
$timespan = $null
switch -Regex ($durationString) {
"^PT(?<seconds>\d*.\d*)S$" {
$timespan = New-TimeSpan -Seconds $matches["seconds"]
}
"^PT(?<minutes>\d*)M(?<seconds>\d*.\d*)S$" {
$timespan = New-TimeSpan -Minutes $matches["minutes"] -Seconds $matches["seconds"]
}
"^PT(?<hours>\d*)H(?<minutes>\d*)M(?<seconds>\d*.\d*)S$" {
$timespan = New-TimeSpan -Hours $matches["hours"] -Minutes $matches["minutes"] -Seconds $matches["seconds"]
}
}
if($timespan -eq $null){
$message = "unhandled duration format '$durationString'"
throw $message
}
$timespan
}
#(ParseOperationDuration "PT21.501S").ToString()
#(ParseOperationDuration "PT5M21.501S").ToString()
#(ParseOperationDuration "PT1H5M21.501S").ToString()
#(ParseOperationDuration "PT 21.501S").ToString() # throws
function Parse-DeploymentOperationSummary{
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$True)]
[object[]] $deploymentOperations
)
process{
$deploymentOperations | %{
$timeStamp = [System.DateTime]::Parse($_.Properties.Timestamp);
$duration = (ParseOperationDuration $_.Properties.Duration);
[PSCustomObject]@{
"Id"=$_.OperationId;
"ProvisioningState" = $_.Properties.ProvisioningState;
"ResourceType"=$_.Properties.TargetResource.ResourceType;
"ResourceName"=$_.Properties.TargetResource.ResourceName;
"StartTime" = $timeStamp - $duration;
"EndTime" = $timeStamp;
"Duration" = $duration;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment