devalored new PowerShell

Kalaimani Thirupathi 411 Reputation points
2021-02-08T15:38:16.843+00:00

Dear All,

Can you someone help me to Get Azure VM snapshot list for unmanaged blob storage using PowerShell.

I have developed the below one but not working.

$subs = Get-AzSubscription
Foreach ($sub in $subs)
{
select-AzSubscription $sub | Out-Null
$subName = $sub.Name
<#$resg = Get-AzResourceGroup
$resgnames = $resg.ResourceGroupName
foreach ($resgname in $resgnames)
{

>

$sa= Get-AzStorageAccount
$sanames= $sa.StorageAccountName
$resgnames = $sa.ResourceGroupName
foreach ($saname in $sanames)
{
$con = Get-AzRmStorageContainer -ResourceGroupName $resgname -StorageAccountName $saname
$Containers =$con.name
foreach ($Container in $Containers)
{
$blobs = Get-AzStorageContainer -Name $Container | Get-AzStorageBlob -IncludeDeleted
}
}} $blobs |Export-Csv \repport.csv

Azure Container Instances
Azure Container Instances
An Azure service that provides customers with a serverless container experience.
651 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,146 Reputation points Microsoft Employee
    2021-02-08T23:43:14.163+00:00

    Hello @Kalaimani Thirupathi ,
    Thanks for your query.

    In order to interact with unmanaged blobs you will have to rely on Azure (not AzureRM) classic powershell CMDlet's

    You can install by :
    Install-Module Azure -AllowClobber

    Can you try using the below sample code to get the snap shot of a blob (Then based upon that you can expand it further)

    $storageContext = New-AzureStorageContext -StorageAccountName "saname" -StorageAccountKey storageaccountkey
    $blobRef = Get-AzureStorageBlob -Context $storageContext-Container ContainerName -blob BlobName
    $snapshots = ($blob.ICloudBlob).Snapshot() //It will list out the snap shots

    I just did a sample run through of those commands and example outputs below are from one of my storage account:

    PS C:\azure> $snapshots = ($blob.ICloudBlob).Snapshot()

    ServiceClient : Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient
    StreamMinimumReadSizeInBytes : 4194304
    Properties : Microsoft.WindowsAzure.Storage.Blob.BlobProperties
    Metadata : {}
    Uri : https://shclassic.blob.core.windows.net/vhd/aspnet.yml
    StorageUri : Primary = 'https://shclassic.blob.core.windows.net/vhd/aspnet.yml'; Secondary = ''
    SnapshotTime : 2/8/2021 11:15:26 PM +00:00
    IsSnapshot : True
    IsDeleted : False
    SnapshotQualifiedUri : https://shclassic.blob.core.windows.net/vhd/aspnet.yml?snapshot=2021-02-08T23:15:26.1385889Z
    SnapshotQualifiedStorageUri : Primary = 'https://shclassic.blob.core.windows.net/vhd/aspnet.yml?snapshot=2021-02-08T23:15:26.1385889Z'; Secondary = ''
    CopyState :
    Name : aspnet.yml
    Container : Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
    Parent : Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory
    BlobType : BlockBlob

    Additional References:

    Try to modify the below code accordingly

    Below module will list out all the UnManaged Disks , tweak the code in TODO section

    Function ListAllUnManagedDisks()
    {
    #initialize the subscriptionid
    $subID = "SubID"
    #login to Azure Account
    Connect-AzAccount
    #Get all Azure storage accounts under the subscription
    $AllStorageAccounts = Get-AzStorageAccount
    #Loop through each storage account
    Write-Host "Looping through all the Storage Accounts under the subscription"
    foreach($stAcc in $AllStorageAccounts)
    {
    #Get the Storage account details under the resource group
    $strAccount = Get-AzStorageAccount -ResourceGroupName $stAcc.ResourceGroupName -Name $stAcc.StorageAccountName
    #Get the context
    $context = $strAccount.Context
    #Get the Container
    $Containers = Get-AzStorageContainer -Context $context
    #Loop through all the containers
    foreach($container in $containers)
    {
    #Get all the blobs in the container
    $blobData = Get-AzStorageblob -Container $container.Name -Context $context
    #Loop through the blobs
    foreach($blob in $blobData)
    {
    #Check if the blob has file with .vhd extension (any blob with extension .vhd considered as UnManaged disk)
    if ($blob.Name.ToString().Contains(".vhd"))
    {
    Write-host "StorageAccountName:" $stAcc.StorageAccountName
    Write-host "ContainerName:" $container.Name
    Write-host "BlobName:" $blob.Name
    #TODO: Try to add above 3 lines of piece of code here ;
    }
    }
    }

    }  
    

    }

    If the above code helps out , kindly "Accept the Answer" so that it will help community out there

    0 comments No comments

0 additional answers

Sort by: Most helpful