Changing The MAC Address Of NIC Using The Hyper-V WMI V2 Namespace

Relating to my previous posts regarding Adding a Network Adapter To A VM Using The Hyper-V WMI V2 Namespace and Connecting a VM Network Adapter To A Switch Using The Hyper-V WMI V2 Namespace I had some questions come in around configuring static vs dynamic MAC addresses.  So here’s some samples – you will notice they are identical except for setting the StaticMacAddress and Address properties.

 

Changing The MAC Address To Static

 $vmName = "Test" 

 #Retrieve the Hyper-V Management Service, ComputerSystem class for the VM and the VM’s SettingData class. 
$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) | % {$_}) 


#Retrieve the NetworkAdapterPortSettings Associated to the VM. 
$Msvm_SyntheticEthernetPortSettingData = ($Msvm_VirtualSystemSettingData.GetRelated(` 
     "Msvm_SyntheticEthernetPortSettingData") `
      | Where-Object {$_.ElementName -eq "Network Adapter"}) 

#Set the Static Mac Address To True and the Address to the MAC for the NIC
$Msvm_SyntheticEthernetPortSettingData.StaticMacAddress = $true
$Msvm_SyntheticEthernetPortSettingData.Address = "0003FF123456"

$Msvm_VirtualSystemManagementService.ModifyResourceSettings($Msvm_SyntheticEthernetPortSettingData.GetText(2))

 

Changing The MAC Address To Dynamic

 $vmName = "Test" 

 #Retrieve the Hyper-V Management Service, ComputerSystem class for the VM and the VM’s SettingData class. 
$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) | % {$_}) 


#Retrieve the NetworkAdapterPortSettings Associated to the VM. 
$Msvm_SyntheticEthernetPortSettingData = ($Msvm_VirtualSystemSettingData.GetRelated(` 
     "Msvm_SyntheticEthernetPortSettingData") `
      | Where-Object {$_.ElementName -eq "Network Adapter"}) 

#Set the Static Mac Address To False and the Address to an Empty String
$Msvm_SyntheticEthernetPortSettingData.StaticMacAddress = $false
$Msvm_SyntheticEthernetPortSettingData.Address = ""

$Msvm_VirtualSystemManagementService.ModifyResourceSettings($Msvm_SyntheticEthernetPortSettingData.GetText(2))

-Taylor Brown

-Program Manager, Hyper-V