Audit number of VHDs per Storage Account | Azure

Time for some code.  I was recently asked by a customer to help them audit the number of active VHDs in a storage account.

As ever with a little digging around and some slight adjustment I was able to provide what they were after.

Original came from the very accomplished John Savill and was posted at Windows IT Pro.

$FindStorage = Get-AzurermStorageAccount 
$out = @()
 
Foreach ($Storage in $FindStorage)
{
$Name = $Storage.StorageAccountName
$ResourceGroupName = $Storage.ResourceGroupName
$Location = $Storage.Location

$AllBlobs = Get-AzureRMStorageAccount -Name $Name -ResourceGroupName $ResourceGroupName | 
    Get-AzureStorageContainer | where {$_.Name -eq 'vhds'} | Get-AzureStorageBlob | where {$_.Name.EndsWith('.vhd')} 

$VHDsinAct = 0

foreach ($Blob in $AllBlobs)
{

    if($Blob.ICloudBlob.Properties.LeaseState -eq 'Leased' -and $Blob.ICloudBlob.Properties.LeaseDuration -eq 'Infinite')
    {        
        $VHDsinAct++ 
    }
}

$props = @{

StorageAccount = $Name
VHDs = $VHDsinAct
ResourceGroup = $ResourceGroupName
Location =$Location
}
 #Write-Output "Total of $VHDsinAct VHDs in $Name"
 $out += New-Object PsObject -Property $props
}

$out | Format-Table -AutoSize -Wrap  StorageAccount, VHDs, ResourceGroup, Location 
$out | Out-GridView -Passthru

Disclaimer:  Please note although I work for Microsoft the information provided here does not represent an official Microsoft position and is provided as is.

(Visited 270 times, 35 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *