Home > Software design >  Get all resources attached to an Azure VM
Get all resources attached to an Azure VM

Time:12-09

I need to get all disks and NICs that one VM have into my Azure, Is there any powershell command to get this info ?

CodePudding user response:

You can use the below Powershell Cmdlets to pull the list of attached datadisk & Network Interfaces for a particular VM.

$rg=<ResourceGroupName>
$name=<virtualMachine> 

((get-azvm -ResourceGroupName $rg -Name $name).StorageProfile).DataDisks

((get-azvm -ResourceGroupName $rg -Name $name).NetworkProfile).NetworkInterfaces

Here is the sample screenshot output for reference:

enter image description here

CodePudding user response:

This is PowerShell code using Azure PowerShell to get the OS disk, data disks, and NICs associated to a VM:

# Set subscription
Set-AzContext -SubscriptionId $subscriptionId

# All VMs
$vms = Get-AzVm

# Targeted VM
#$vms = Get-AzVm -ResourceGroupName $resourceGroupName -Name $vmName

$vms | foreach {

  # VM name
  $_.Name

  # OS Disk
  $_.StorageProfile.OsDisk

  # Data Disks
  $_.StorageProfile.DataDisks | foreach { $_ }

  # NICs
  $_.NetworkProfile.NetworkInterfaces | foreach { $_ }

}
  • Related