Hyper-V: How to unbind a physical NIC from a Virtual Switch using WMI and PowerShell
If you`re not already familiar with networking in Microsoft Hyper-V I would recommend you to have a look at this whitepaper from Microsoft, which described how networking works in Hyper-V.
The following solution will describe a problem which might occur when configuring virtual networks in Hyper-V. Consider the following scenario:
- You`re about to configure a new external virtual network in Hyper-V using Hyper-V Manager remotely from another computer. This is a common scenario when working with the Core edition of Windows Server 2008/2008 R2.
- When selecting the physical NIC to bind to the new virtual network, you choose the adapter which you are remotely connecting to the Hyper-V host through.
What happens in this scenario is that the Virtual Switch Management Service is binding the external Ethernet port for the selected NIC to the Microsoft Windows Virtualization network subsystem. What normally should happen next is that the converted Ethernet port should be bound to the new virtual switch you are creating. However, this never happens since the NIC you are remotely managing the Hyper-V host through is no longer available in the parent operating system. This leaves the NIC in an “orphaned” state, since you cannot use the NIC in the parent operating system, and it`s not in use by any virtual networks.
To resolve this issue, whether using the full GUI version or the Core version of Windows Server, you need to manually unbind the the Ethernet port. There is an UnbindExternalEthernetPort available on the Msvm_VirtualSwitchManagementService WMI class, which is fully documented in this article on MSDN.
To invoke the WMI method we can use Windows PowerShell. To ease the procedure I`ve created a PowerShell function you can use if you ever come into the need for manually unbinding an external Ethernet port in Hyper-V:
Function Select-List
{
Param ([Parameter(Mandatory=$true ,valueFromPipeline=$true )]$InputObject,
[Parameter(Mandatory=$true)]$Property)
begin { $i= @() }
process { $i += $inputobject }
end { if ($i.count -eq 1) {$i[0]} elseif ($i.count -gt 1) {
$Global:counter=-1
$Property=@(@{Label=“ID”; Expression={ ($global:Counter++) }}) + $Property
format-table -inputObject $i -autosize -property $Property | out-host
$Response = Read-Host “Select NIC to unbind”
if ($response -gt “”) {
$I[$response]
}
}
}
}
function Remove-HVExternalEthernetPort {
$ExternalEthernetPort = Get-WMIObject -class “Msvm_ExternalEthernetPort” -namespace “root\virtualization” | Select-List -Property name
$HVSwitchObj = Get-WMIObject -class “MSVM_VirtualSwitchManagementService” -namespace “root\virtualization”
if ($ExternalEthernetPort) {
$HVSwitchObj.UnbindExternalEthernetPort()
}
else {
throw “An error occured. Choose a valid ExternalEthernetPort from the provided list”
}
}
Note: The Select-List function is a modified version of the Select-List function available in the PowerShell Management Library for Hyper-V available on CodePlex (see link below).
You can either paste the function into a PowerShell session or save it into ps1-file and dot source it. When done you can invoke the function like this:
When you`ve entered the index number for the NIC you want to remove, a return value of 0 indicates the operation succeeded. Any other value indicates an error (look at the previous mentioned MSDN-article for more information).
More resources on managing Hyper-V using PowerShell
PowerShell Management Library for Hyper-V – this is an excellent PowerShell module for managing Hyper-V available on CodePlex
System Center Virtual Machine Manager 2012: Scripting
System Center Virtual Machine Manager 2008 R2: Scripting
Getting starting with Cisco UCS PowerShell Toolkit
Cisco – widely known as a networking infrastructure vendor – entered the blade server market in 2009. Their offering is called Unified Computing System, described by Gartner as a fabric-enabled, enterprise-class platform with good integration of networking, virtualization, management tools and storage. On the 2011 Gartner Magic Quadrant for Blade Servers they`re defined as a visionary vendor, and it will be interesting to see if they can challenge the 3 leaders Dell, IBM and HP.
The Cisco Unified Computing System is quite different from traditional blade systems, in that the server profiles (so called service profiles) is independent from the physical blade servers. An example to describe what this means is that a physical blade server might fail and be replaced, while the server profile keeps unique ID`s like MAC addresses, World Wide Names (WWN`s) and so on. If using boot from SAN rather than local disk drives, physical interaction is not required to get the system back online if a spare blade server is available.
The UCS core components
- Blade Chassis – Blade server enclosure
- Cisco UCS Manager – Embedded into the Fabric Interconnect. Provides management capabilities
- Cisco UCS fabric interconnect – Provides networking (Ethernet/Fibre Channel) and management for connected blades and chassis`
- Fabric Extenders – Provides connection between the interconnect fabric and the blade enclosures
A photo showing the Cisco Unified Computing System architecture is available here (cisco.com). In regards of management, all aspects of Cisco UCS can be managed through an XML API. This makes it possible for 3rd parties to offer management solutions, and integration with other products. An example of a 3rd party product is the Cisco UCS Iphone/Ipad application for managing and monitoring the system. Links for more information on the management model and the XML API is available in the resources section in the bottom of this article.
Cisco UCS PowerShell Toolkit
Based on a customer request from an early adaptor, Cisco provided PowerShell support for managing their UCS product through the XMP API. With the Microsoft automation strategy in mind, this was an excellent choice. It will make integration into products like System Center Orchestrator (formerly Opalis) very easy, and the also make the product attractive for enterprises. The PowerShell administration tool is available as a module part of the Cisco UCS PowerShell Toolkit.
A great way to learn using the Cisco UCS PowerShell Toolkit is downloading the Cisco UCS Emulator. This is a virtual machine image which can be imported into VMware Player or VirtualBox. When the VM is up and running you can access both the UCS Manager (http URL is shown when the VM has started) and the XML API. A great feature is that you can import the configuration from a production UCS to simulate administration changes in a lab environment.
Next, you can download the latest versions of both the Cisco UCS PowerShell Toolkit (aka UCSMPowerTool) and the PowerShell Toolkit User Guide from this website.
The installer will by default put the files in %programfiles%\Cisco\UCSMPowerToolkit:
CiscoUCSPS.dll is the assembly which can be imported as a module in Windows PowerShell 2.0 and later. You can either double-click LaunchUCSPS.bat or invoke StartUCSPS.ps1 from an existing PowerShell session to get started. Alternatively you can use Import-Module from an existing PowerShell session like this:
Import-Module “C:\Program Files (x86)\Cisco\UCSMPowerToolkit\CiscoUCSPS.dll”
I would suggest rather than installing to the Program Files folder (which requires administrative privileges), that Cisco generates a module manifest and install the module to the default PowerShell module directory (C:\Users\<username>\Documents\WindowsPowerShell\Modules).
When the module is imported we can use Get-Command with the –Module parameter to list all command inside the CiscoUCSPS module:
The current version of the module contains 149 cmdlets, so all commands are not shown in the above screenshot.
The first thing we need to do is connect to an instance of the UCS Manager. If using the Cisco UCS Emulator you can view the management IP address when the virtual machine has started:
We then use the Connect-UCSM cmdlet to connect to the UCS Manager:
The default credentials for the UCS Emulator is config/config.
Next we can start exploring cmdlets like Get-Blade and Get-Vlan. Note that by default the cmdlets outputs a lot of information for each object, so I`ve picked out a few properties to show using Format-Table:
Going further it`s easy to automate things like adding VLAN and assigning it to a vNIC template:
Although the PowerShell coverage isn`t 100% yet, it`s possible to administer all aspect of the UCS directly through the XML API from PowerShell using the Invoke-XmlCommand cmdlet.
Resources
A Platform Built for Server Virtualization: Cisco Unified Computing System
Cisco Unified Computing Forums
Automating Cisco UCS Management with Windows PowerShell
PowerScripting Podcast – Josh Heller from Cisco on UCS and PowerShell
Cisco UCS Manager XML API Programmer’s Guide
Cisco UCS Manager API Management Information Model
System Center Virtual Machine Manager 2008 and PowerShell
I`ve been playing around with System Center VMM 2008 lately, and I really like “View Script”-button:
Clicking on the button you get a text-file containing the script which is being run when you press “Finish”. (Almost the same functionality in Exchange 2007, the difference is that you must press Ctrl + C rather than a button to copy the script/command being run in the Exchange Management Console).
Sample textfile-output:
# ------------------------------------------------------------------------------ # New Virtual Machine Script # ------------------------------------------------------------------------------ # Script generated on 18. desember 2008 18:46:03 by Virtual Machine Manager # # For additional help on cmdlet usage, type get-help <cmdlet name> # ------------------------------------------------------------------------------ New-VirtualNetworkAdapter -VMMServer localhost -JobGroup 6928344f-7732-4e1b-b852-15cf0288cd2f -PhysicalAddressType Dynamic -VLanEnabled $false New-VirtualDVDDrive -VMMServer localhost -JobGroup 6928344f-7732-4e1b-b852-15cf0288cd2f -Bus 1 -LUN 0 $CPUType = Get-CPUType -VMMServer localhost | where {$_.Name -eq "1.20 GHz Athlon MP"} New-HardwareProfile -VMMServer localhost -Owner "RBK\jer" -CPUType $CPUType -Name "Profiledb3743da-b115-4c6b-bab5-6cbf89fde1ad" -Description "Profile used to create a VM/Template" -CPUCount 1 -MemoryMB 512 -RelativeWeight 100 -HighlyAvailable $false -NumLock $false -BootOrder "CD", "IdeHardDrive", "PxeBoot", "Floppy" -LimitCPUFunctionality $false -JobGroup 6928344f-7732-4e1b-b852-15cf0288cd2f New-VirtualDiskDrive -VMMServer localhost -IDE -Bus 0 -LUN 0 -JobGroup 6928344f-7732-4e1b-b852-15cf0288cd2f -Size 40960 -Dynamic -Filename "Demo-01_disk_1" $VMHost = Get-VMHost -VMMServer localhost | where {$_.Name -eq "HYPER-V-JR.rbk.ad"} $HardwareProfile = Get-HardwareProfile -VMMServer localhost | where {$_.Name -eq "Profiledb3743da-b115-4c6b-bab5-6cbf89fde1ad"} $OperatingSystem = Get-OperatingSystem -VMMServer localhost | where {$_.Name -eq "Windows Server 2008 Standard 32-Bit"} New-VM -VMMServer localhost -Name "Demo-01" -Description "" -Owner "RBK\jer" -VMHost $VMHost -Path "D:\Hyper-V" -HardwareProfile $HardwareProfile -JobGroup 6928344f-7732-4e1b-b852-15cf0288cd2f -RunAsynchronously -OperatingSystem $OperatingSystem -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM
Now you can save it as a ps1-file and tweak as wanted. This makes it really easy to get started automating tasks in SCVMM.
Btw, SC VMM 2008 is a really great product, making it possible to manage Virtual Server 2005, Hyper-V and VMWare ESX




