none
Delete storage accounts

    Question

  • Hi all,

    I have multiple storage accounts and multiple VMs. But  I have deleted few VMs and as deleting VM doesn't delete storage account automatically. So can you please tell how to check which storage is associated to which VM and then I can delete those storage account.

    Many thanks in advance.


    Please Mark as Answer if my post works for you or Vote as Helpful if it helps you. Kapil Singh

    Wednesday, April 26, 2017 9:03 AM

All replies

  • Hi,

    you can this (quick and dirty) PowerShell script to get a list of VMs with their associated storage accounts:

    Login-AzureRmAccount
    ForEach ($vm in (Get-AzureRmVm))
    {
        # Initialize an empty array
        $accounts = @()
    
        # Get the OS disk location
        $osDisk = $vm.StorageProfile.OsDisk.Vhd.Uri
        $accounts += $osDisk.Substring($osDisk.IndexOf("//") + 2, $osDisk.IndexOf(".") - $osDisk.IndexOf("//") - 2)
    
        # Get storage account for diagnostics
        $diag = $vm.DiagnosticsProfile.BootDiagnostics.StorageUri
        If ($diag -ne $null -or $diag -ne "")
        {
            $account = $diag.Substring($diag.IndexOf("//") + 2, $diag.IndexOf(".") - $diag.IndexOf("//") - 2)
            If ($accounts -notcontains $account)
            {
                $accounts += $account
            }
        }
    
        # Get all data disks
        ForEach ($disk in $vm.StorageProfile.DataDisks)
        {
            $uri = $disk.Vhd.Uri
            $account = $uri.Substring($uri.IndexOf("//") + 2, $uri.IndexOf(".") - $uri.IndexOf("//") - 2)
            If ($accounts -notcontains $account)
            {
                $accounts += $account
            }
        }
    
        Write-Host ("VM {0}: {1}" -f $vm.Name,($accounts -join ","))
    }
    It will output a list of all VMs in the current subscription with the storage accounts separated by comma. It looks for OS disk, Boot Diagnostics and Data Disks. If you need more information, let me know and I'll update the script.


    Floris van der Ploeg - www.florisvanderploeg.com

    If my post was helpfull, remember to click the "Propose as answer" button.

    Wednesday, April 26, 2017 11:56 AM