I am trying to find PowerShell cmdlet which can retrieve information about which storage account Azure VM use.
Example, I want to supply name of VM and I would like to see which storage that VM use. Or it will be also good to query specific storage account and see which VMs use this storage account.
I am trying following cmdlets but I cannot see details about storage account:
Select-AzureSubscription -SubscriptionName "name"
Get-AzureVM -Name "name" -ServiceName "name"
The Get-AzureDisk cmdlet may be useful for you. This is the approach that I'm using.
$disk = Get-AzureDisk | Where-Object { $_.AttachedTo.RoleName -eq "YOURVMNAME" }
$mediaLink = $disk.MediaLink
$storageAccountName = $mediaLink.Host.Split('.')[0]
https://msdn.microsoft.com/en-us/library/azure/dn495125.aspx
I am answering the second part of your question. You want list of VM under a particular Storage Account. Suppose your storage account name is "xyz123" and you want to list all vms under this storage account. Then you just need to run the script given below-
$vms = Get-AzureVM
$output = " "
$tgtStorageaccount = "xyz123"
foreach($vm in $vms)
{
$disk = Get-AzureVM -ServiceName $vm.ServiceName –Name $vm.Name | Get-AzureOSDisk
$mediaLink = $disk.MediaLink
$storageAccountName = $mediaLink.Host.Split('.')[0]
if ($storageAccountName -eq $tgtStorageaccount)
{
$output =$output + "`r`n" + $vm.Name
}
}
$output
Hope This one will help you. Thanks.
Yes. Storageaccount
can be find for RM based VM.
For OS:
$output = Get-AzureRmVM -ResourceGroupName "xx-RG-xx-DEv" -Name "xx-xx-vm-DEV"
$storageAccountName = $output.StorageProfile.OsDisk.Vhd.Uri.Split("/")[2].Split(".")[0]
Get-AzureRmStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName "xx-RG-xx-DEv"
Each of a vm's disks is going to be stored in a given blob, with the blob's uri containing the storage account name. For example:
https://mystorageaccount.blob.core.windows.net/vhds/myosdisk.vhd
You'd need to retrieve the various os disk and data disk uri's from your vm's and then parse the uri accordingly. Here's a way to get the storage account for each disk by first grabbing the base uri (mystorageaccount.blob.core.windows.net
) and then taking the first string segment (separated by dot characters).
For the OS disk:
Get-AzureVM -ServiceName myservice -Name myvmname |
GetAzureOSDisk |
ForEach-Object { $_.MediaLink.Host.split(".")[0] }
And likewise for data disks:
Get-AzureVM -ServiceName myservice -Name myvmname |
GetAzureDataDisk |
ForEach-Object { $_.MediaLink.Host.split(".")[0] }
You'll get duplicate storage account names if you used the same storage account for more than one disk.