question

MAHESHNIKAM-2436 avatar image
0 Votes"
MAHESHNIKAM-2436 asked TimCerling-5039 commented

Hyper-V - Fetch Network-Adapter related information(MACAddress, Switch, Name, IPaddress) Through WMI query

I want to fetch following Virtual Machine related information through WMI:
Network Adapter:

  1. MACaddress

  2. IPaddress

  3. SwitchName

  4. Name

Same information I can fetch through powershell:

Get-VM -Id '{VM-ID}' | Get-VMNetworkAdapter | Select IPAddresses, SwitchName, MacAddress, Name

But I want to achieve same through WMI queries.

Please suggest me best approach



windows-server-powershellwindows-server-hyper-vwindows-server-management
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLaude avatar image
1 Vote"
LeonLaude answered TimCerling-5039 commented

Hi @MAHESHNIKAM-2436,

I'm no WMI expert but these different commands below will get you the following:

  • IP addresses

  • Virtual Switch name

  • MAC Address

  • Network Adapter name



Gets the network adapter name & MAC address of the Hyper-V guest virtual machine

 $VMName = "VMname"
 $VMNet = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$VMName'"
 $VMNet.GetRelated('Msvm_SyntheticEthernetPort') | Select ElementName, PermanentAddress

Gets the Hyper-V switch name of the Hyper-V guest virtual machine

 $VMName = "VMname"
 $VMSwitch = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$VMName'"
 $VMSwitch.GetRelated('Msvm_SummaryInformation') | Select VirtualSwitchNames

Gets the IP addresses of the Hyper-V guest virtual machine

 $VMName = "VMname"
 $Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService 
 $Msvm_ComputerSystem = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$VMName'"
 $Msvm_VirtualSystemSettingData = ($Msvm_ComputerSystem.GetRelated("Msvm_VirtualSystemSettingData","Msvm_SettingsDefineState", $null, $null, "SettingData", "ManagedElement", $false, $null) | % {$_})
 $Msvm_SyntheticEthernetPortSettingData = $Msvm_VirtualSystemSettingData.GetRelated("Msvm_SyntheticEthernetPortSettingData")
 $Msvm_GuestNetworkAdapterConfiguration = ($Msvm_SyntheticEthernetPortSettingData.GetRelated("Msvm_GuestNetworkAdapterConfiguration"))
 $Msvm_GuestNetworkAdapterConfiguration | Select IPAddresses


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)


Best regards,
Leon

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Leon,

Thanks for efforts,

But want to achieve like below through WMI:

PS> Get-VM 'BSB-Win7' | Get-VMNetworkAdapter | Select IPAddresses, SwitchName, MacAddress, Name

IPAddresses SwitchName MacAddress Name


{172.26.10.188, fe80::75e2:ae27:3a8c:3b3b} VswitchGui256 00155D08A39F Network Adapter
{169.254.60.45, fe80::dd16:6be9:3be6:3c2d} k8s-infra 00155D08A33F Network Adapter

0 Votes 0 ·

PowerShell is Microsoft easy to understand interface to WMI. Is there some reason you want to use WMI directly instead of using PowerShell? Is there a reason it must be native WMI instead of the recommended PowerShell?

0 Votes 0 ·