Get storage account of Azure VM

痞子三分冷 提交于 2019-12-11 01:00:07

问题


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"

回答1:


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




回答2:


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.




回答3:


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"



回答4:


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.



来源:https://stackoverflow.com/questions/28997810/get-storage-account-of-azure-vm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!