Skip to content

Instantly share code, notes, and snippets.

@Romiko
Created February 8, 2012 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romiko/1763916 to your computer and use it in GitHub Desktop.
Save Romiko/1763916 to your computer and use it in GitHub Desktop.
UndeployAzure
#requires -version 2.0
param (
[parameter(Mandatory=$true)] [string]$AzureServiceName,
[parameter(Mandatory=$true)] [string]$AzureDeploymentSlot,
[parameter(Mandatory=$true)] [string]$AzureSubscriptionId,
[parameter(Mandatory=$true)] [string]$AzureCertificateThumbprint)
$ErrorActionPreference = "Stop"
if ((Get-PSSnapin -Registered -Name AzureManagementCmdletsSnapIn -ErrorAction SilentlyContinue) -eq $null)
{
throw "AzureManagementCmdletsSnapIn missing. Install them from Https://www.cerebrata.com/Products/AzureManagementCmdlets/Download.aspx"
}
Add-PSSnapin AzureManagementCmdletsSnapIn -ErrorAction SilentlyContinue
function CheckIfDeploymentIsDeleted
{
$triesElapsed = 0
$maximumRetries = 10
$waitInterval = [System.TimeSpan]::FromSeconds(30)
Do
{
$triesElapsed+=1
[System.Threading.Thread]::Sleep($waitInterval)
Write-Verbose "Checking if deployment is deleted, current retry is $triesElapsed/$maximumRetries"
$deploymentInstance = Get-Deployment `
-ServiceName $AzureServiceName `
-Slot $AzureDeploymentSlot `
-SubscriptionId $AzureSubscriptionId `
-Certificate $certificate `
-ErrorAction SilentlyContinue
if($deploymentInstance -eq $null)
{
Write-Verbose "Deployment is deleted"
break
}
if($triesElapsed -ge $maximumRetries)
{
throw "Checking if deployment deleted has been running longer than 5 minutes, it seems the delployment is not deleting, giving up this step."
}
}
While($triesElapsed -le $maximumRetries)
}
Write-Verbose "Retrieving management certificate"
$certificate = Get-ChildItem -Path "cert:\CurrentUser\My\$AzureCertificateThumbprint" -ErrorAction SilentlyContinue
if ($certificate -eq $null)
{
throw "Couldn't find the Azure management certificate in the store"
}
if (-not $certificate.HasPrivateKey)
{
throw "The private key for the Azure management certificate is not available in the certificate store"
}
$currentDeploymentInstance = Get-Deployment `
-ServiceName $AzureServiceName `
-Slot $AzureDeploymentSlot `
-SubscriptionId $AzureSubscriptionId `
-Certificate $certificate `
-ErrorAction SilentlyContinue
if($currentDeploymentInstance -eq $null)
{
Write-Verbose "Deployment is already deleted"
}
else
{
Write-Verbose "Deleting Deployment"
Remove-Deployment `
-ServiceName $AzureServiceName `
-Slot $AzureDeploymentSlot `
-SubscriptionId $AzureSubscriptionId `
-Certificate $certificate `
-ErrorAction SilentlyContinue
Write-Verbose "Sent delete deployment intructions asynchronously, will check back later to see if it is deleted"
CheckIfDeploymentIsDeleted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment