I need help with listing all Azure Virutal Machines in a subscription with their associated storage account name.
I have tried the below command but am not getting the expected output:
Get-Azurermvm | Ft vmname,storageaccount,location
I need help with listing all Azure Virutal Machines in a subscription with their associated storage account name.
I have tried the below command but am not getting the expected output:
Get-Azurermvm | Ft vmname,storageaccount,location
Below script will give you the VM Name along with Resource group Name, Subscription name ,disk name, disk type and the storage account associated with it.
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
}
}
}
}