Skip to content

Instantly share code, notes, and snippets.

@rheid
Created December 7, 2016 13:16
Show Gist options
  • Save rheid/1be1eab8658b7e4a7b60e9b114a0f673 to your computer and use it in GitHub Desktop.
Save rheid/1be1eab8658b7e4a7b60e9b114a0f673 to your computer and use it in GitHub Desktop.
Remove Azure VM Full
# Login and select Azure Subscription.
Login-AzureRmAccount -TenantId db87e8a4-ab1f-4a72-844d-f9ad89ad4258
Select-AzureRmSubscription -SubscriptionId 72e4639f-8f07-45c5-9347-9e3dd84691db
# Get all Azure VM's in subscription so they can be added to drop down list.
$listvms = Get-AzureRmVM
$extractvmname = $listvms.Name
# Add All VM's in Subscription to drop down list.
[array]$DropDownArray = $extractvmname
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
}
function selectShare{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select Azure VM to Remove"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$vmname = selectShare
# Extract Resourcegroupname, full nic path and full path to VHD from selected VM
#
# VHD storage path/url will look like: https://storageaccountname.blob.core.windows.net/vhds/xxxxxx.vhd
# NIC name will look like: "/subscriptions/subscriptionidxxxx/resourceGroups/resourcegroupname/providers/Microsoft.Network/networkInterfaces/nicname
#
#
$listvmsforrg = Get-AzureRmVM
$vmtoextractrg = $listvmsforrg | Where-Object { $_.Name -eq $vmname }
$resourcegroup = $vmtoextractrg.ResourceGroupName
$setnictoremove = $vmtoextractrg.NetworkInterfaceIDs
$setdisktoremove = $vmtoextractrg.StorageProfile.OsDisk.Vhd.Uri
# Extract nicname, storage account and vhdname from returned strings.
# Assuming Azure VM VHD is in VHDS contrainer within extracted storage account.
$S1 = $setnictoremove
$S2 = $setdisktoremove
$nicname = Split-Path $setnictoremove -Leaf
$vhdname = Split-Path $setdisktoremove -Leaf
$storageaccountname = $setdisktoremove.Substring($S2.Indexof("://")+3,($S2.Indexof(".")-$S2.Indexof("://")-3))
Remove-AzureRmVM -ResourceGroupName $resourcegroup -Name $vmname
$fullkeys = Get-AzureRmStorageAccountKey -StorageAccountName $StorageAccountName -ResourceGroupName $resourcegroup
$key = $fullkeys[0].key1
$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $key
Remove-AzureStorageBlob -Container vhds -Blob $vhdname -Context $context
Remove-AzureRmNetworkInterface -ResourceGroupName $resourcegroup -name $nicname
$statusfile = Get-AzureStorageBlob -Container "vhds" -Context $context -Verbose
$statusfile2 = $statusfile | Where-Object { $_.Name.Contains($vmname) }
Remove-AzureStorageBlob -Container vhds -Blob $statusfile2.Name -Context $context
@wiemerr
Copy link

wiemerr commented Dec 14, 2016

To make this work I had to change line 96 to:
$key = $fullkeys[0].value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment