Removing a Network Adapter To A VM 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 removing a NIC. So here’s a sample – it’s pretty simple…
Removing a NIC
$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"})
#Call the RemoveResourceSettings function with the path to network adapater
$Msvm_VirtualSystemManagementService.RemoveResourceSettings($Msvm_SyntheticEthernetPortSettingData)
-Taylor Brown
-Program Manager, Hyper-V