Powershell列出具有存储帐户名称的Azure VM

时间:2017-03-31 07:08:58

标签: powershell azure-vm-role azure-storage-account

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

我已尝试过以下命令,但未获得预期的输出:

Get-Azurermvm | Ft vmname,storageaccount,location

1 个答案:

答案 0 :(得分: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
}
}
}
}