As an Azure administrator, it’s your duty to keep all of your servers up-to-date and running smoothly. Removing a VM from the system is an easy task with PowerShell commands that take little time at all.
This article will show you how to delete an Azure VM and cleanup with PowerShell. The article also includes a link to Microsoft’s guide on deleting an Azure VM. Read more in detail here: how to delete azure vm and all associated resources powershell.
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$vm.StorageProfile.OSDisk.Vhd.Uri = $osDiskUri $osDiskUri.Split(‘/’) = $osDiskContainerName [-2]
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
@’ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa ‘Name’ = $diagSa where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
(Get-AzStorageAccount | where
Virtual machines are one of the most prevalent applications of the Microsoft Azure cloud for administrators (VMs). It’s simple to establish Azure VMs, but removing one is more difficult. You’ve come to the correct spot if you need to know how to remove an Azure virtual machine.
Sure, you can just go to the Azure Portal, locate your VM, and delete it. To swiftly delete a VM, you could use the Remove-AzVM PowerShell command from the Azure PowerShell module, but there’s a lot more to that VM than just the VM itself.
When you construct that VM, you may have produced a slew of additional resources that need to be cleaned up as well. The following resources may all be linked to a VM that has to be decommissioned.
- Storage containers for boot diagnostics
- Network connections
- IP addresses that are available to the general public
- Blobs for the OS disk and status storage
- Disks with data
In this post, we’ll show you how to create a Delete-AzrVirtualMachine PowerShell function that will remove not just the Azure VM but also everything directly related with it.
Prerequisites
You’ll need a few items to destroy an Azure VM and clear any other connected resources using PowerShell.
- The PowerShell Module for Azure?- Install-Module Az is a PowerShell module that can be downloaded and installed through the PowerShell Gallery.
- Are you signed in to an Azure subscription?- You’ll need to sign in to the Azure subscription where your VM is hosted. Run Connect-AzAccount to do this.
To verify, you can then run Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName> to check it returns the VM. If no error is returned, you should be good to go.
Obtaining the Azure Virtual Machine Object
To make things easy with our script, we’ll retrieve the VM object first, which has all of the information we need. The Get-AzVm command is used to do this. I’ll be using a VM named WINSRV19 from the MyTestVMs resource group for this post.
Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs $vm = Get-AzVm
As you proceed through the script, you should have the VM object saved in the $vm variable, enabling you to read different things from it.
The Boot Diagnostics Storage Container is being removed.
You always have the option of generating a boot diagnostics container when building an Azure VM. This is important for diagnosing VM boot difficulties, but it is not destroyed when the VM is deleted. Let’s change it.
To delete the boot diagnostics container, you must first identify the storage account on which the container is stored. You’ll have to parse the storageUri attribute hidden in the DiagnosticsProfile object on the VM to identify this storage account.
Here’s some regex to parse out that name to save you some time.
$diagSa = match [regex] ($vm.DiagnosticsProfile.bootDiagnostics.storageUri, ‘^http[s]?://(.+?)\.’).groups[1].value
The next step is to identify the boot diagnostics storage container’s name. To do so, you’ll need to use the Get-AzResource command to retrieve the VM ID. Here’s some PowerShell to help you do that.
$vm.Name.Length -gt 9) if { I = 9 } else I = 1 – $vm.Name.Length $azResourceParams = @ ‘ResourceName’ = WINSRV ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘ResourceGroupName’ = MyTestVMs ‘ResourceType’ = ‘Microsoft.Compute/virtualMachines’ ‘Re @azResourceParams $vmResource = Get-AzResource $vmResource.Properties.VmId = $vmId (‘bootdiagnostics-0-1’ -f) $diagContainerName = $vm.Name.ToLower(). $vmId, $substring(0, I
The name of the resource group that the boot diagnostics container belongs to is the next step.
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $diagSa) $diagSaRg ResourceGroupName
Finally, use the Delete-AzStorageContainer command to remove the storage container.
$saParams = @{ ‘ResourceGroupName’ = $diagSaRg ‘Name’ = $diagSa } Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.Name-eq Get-AzStorageAccount @saParams | Get-AzStorageContainer | Remove-AzStorageContainer -Force $diagContainerName
Down one resource! Continue reading to learn how to remove an Azure virtual machine from disk.
deleting the Azure virtual machine
The virtual machine will then be removed. You may provide that object to the Remove-AzVm command and be done with it since you’ve already captured the VM in the $vm variable.
Remove-AzVM -Force $null = $vm
Removing Network connections and IP addresses that are available to the general public
Next up, are one or more Network connections (NICs) that were once attached to that VM. Using the NetworkInterfaces property on the VM object, we can loop through each ID, remove the NIC with the Remove-AzNetworkInterface command.
We can additionally check the IpConfiguration property on each NIC to see whether it has a public IP address while we’re in this loop. If this is the case, we may use the Delete-AzPublicIpAddress command to remove it.
As you can see below, I’m scanning for all NICs, deleting them one by one, then checking for public IPs on each NIC and, if any are found, parsing the PublicIpAddress property’s ID to extract the name of the public IP address resource and removing it.
$vm.NetworkProfile.NetworkInterfaces.Id.foreach($nicUri) $nicUri.Split(‘/’) = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name [-1] -Name $nic.Name -ResourceGroupName $vm Remove-AzNetworkInterface ResourceGroupName -Force if($ipConfig.PublicIpAddress -ne $null) foreach($ipConfig in $nic.IpConfigurations) Remove-AzPublicIpAddress -Name $ipConfig.PublicIpAddress.Id -ResourceGroupName $vm.ResourceGroupName Split(‘/’)[-1] -Force } } }
Removing the Operating System Disk
The OS disk is not erased when an Azure VM is deleted. The OS disk is a storage blob that you can delete using the Remove-AzStorageBlob command, but you’ll have to do some research first, just like the previous stages, to locate the right parameter values to send to it.
I’m initially looking for the name of the OS disk storage container in the PowerShell code snippet below. Once I’ve discovered it, I’ll use it, together with the storage account on which the OS disk storage container is stored, to send to Delete-AzStorageBlob to remove the OS disk.
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri $osDiskContainerName = $osDiskUri.Split(‘/’)[-2] $osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split(‘/’)[2].Split(‘.’)[0] } $osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split(‘/’)[-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
.StorageAccountName -eq $osDiskStorageAcct = Get-AzStorageAccount $osDiskUri.Split(‘/’)[2]. Split(‘.’) [0] } Remove-AzStorageBlob -Container $osDiskStorageAcct -Blob $osDiskContainerName $osDiskUri.Split(‘/’) [-1]
Removing the Operating System Disk Status Blob
To clear up the OS disk status blog, I need to locate the storage container the OS disk is in, assume the blog ends in status, and then use Remove-AzStorageBlob to remove the blob.
Get-AzStorageBlob -Container $osDiskStorageAcct Remove-AzStorageBlob $osDiskContainerName -Blob “$($vm.Name)*.status”
Removing Attached Disks with data
Finally, some VMs may have Disks with data attached to them that you’d rather not use elsewhere. Not a problem. We can remove those too! This is the final step in deleting an Azure VM and all of it’s components.
First, on the original VM object you obtained, read the Uri attribute contained deep in the StorageProfile object. Because a URI might refer to several data disks, you’ll loop over them all to determine which storage account each one belongs to.
As seen below, after you’ve located each one’s storage account, you’ll utilize it and parse the storage URI to generate the blob name to provide to the Remove-AzStorageBlob command.
If ($vm.DataDiskNames.Count -gt 0) is true, then $uri in $vm.StorageProfile.DataDisks.Vhd.Uri) foreach Get-AzStorageAccount -Name $uri $dataDiskStorageAcct Split(‘/’)[2]. Split(‘.’) [0] Remove-AzStorageBlob -Container $uri | $dataDiskStorageAcct Split(‘/’)[-2] $uri.Split(‘/’)[-1] -Blob
Summary
This article has covered a lot of ground. You could rapidly construct an utility from this code if you followed excellent PowerShell coding techniques. Fortunately for you, I’ve already taken care of it! Check out the Remove-AzrVirtualMachine function for an example of how this code may be used to destroy an Azure virtual machine.
The “azure cli delete vm” is a command-line tool that allows users to delete an Azure VM and cleanup with PowerShell.
Related Tags
- delete azure vm using powershell
- remove-azurermvm
- remove-azdisk
- get-azvm
- how to decommission a server in azure