0

我需要帮助列出订阅中的所有 Azure 虚拟机及其关联的存储帐户名称。

我尝试了以下命令,但没有得到预期的输出:

Get-Azurermvm | Ft vmname,storageaccount,location
4

1 回答 1

0

下面的脚本将为您提供 VM 名称以及资源组名称、订阅名称、磁盘名称、磁盘类型和与之关联的存储帐户。

Login-AzureRmAccount 

$subscription=Get-AzureRmSubscription | select SubscriptionName -ExpandProperty SubscriptionName

foreach($sub in $subscription)
{

Select-AzureRmSubscription -SubscriptionName $sub

$StroageAccDetail = Get-AzureRmStorageAccount

$vmstatus=Get-AzurermVM -Status  

foreach ($vmdetails in $vmstatus)
{
$ResourceGroupName=$vmdetails.ResourceGroupName
$VMName=$vmdetails.name
$storage=$vmdetails.StorageProfile.OsDisk.Vhd.Uri
$DataDiks=$vmdetails.StorageProfile.OsDisk.Name
$ss=$storage.split('/')[2]

$OSStorageAccountName=$ss.split('.')[0]
$DiskType="OSDisk"
$StroageAccountLocation=($StroageAccDetail | where  {$_.StorageAccountName -eq $OSStorageAccountName }).location
$StorageAccountType=($StroageAccDetail | where  {$_.StorageAccountName -eq $OSStorageAccountName }).AccountType
"$sub,$ResourceGroupName,$VMName,$DataDiks,$DiskType,$OSStorageAccountName" | Out-File $OutFile -Append
$vmdatadisks=$vmdetails.StorageProfile.DataDisks

if($vmdatadisks -ne $null){
foreach($vmDatadsik in $vmdatadisks)
{
$vmDatadsikss=$vmDatadsik.vhd.uri
$DiskType="DataDisk"
$ss=$vmDatadsikss.split('/')[2]
$DataDiks=$vmDatadsik.Name
$dataStorageAccountName=$ss.split('.')[0]
"$sub,$ResourceGroupName,$VMName,$DataDiks,$DiskType,$dataStorageAccountName" | Out-File $OutFile -Append
}
}
}
} 
于 2017-03-31T07:26:51.230 回答