In response to a few of my old posts Hyper-V WMI Using PowerShell Scripts – Part 5 (Creating Virtual Switchs/Networks) and Hyper-V V2: Guest Only External Networks + Add Roles Wizard Changes I’ve had a lot of people ask me how to create external virtual switches that do not allow management traffic i.e. guest only external switches or vm only external switches.
So Here you go – do note that I am using my ProcessWMIJob function from my past posting Hyper-V WMI: Rich Error Messages for Non-Zero ReturnValue (no more 32773, 32768, 32700…).
function CreateSwitch { param ( [string] $SwitchName = $null, [string] $PhysicalNICName = $null ) $VirtualSwitchService = Get-WmiObject -Namespace "root\virtualization" -Class "Msvm_VirtualSwitchManagementService" $CreatedSwitch = ($VirtualSwitchService.CreateSwitch([guid]::NewGuid().ToString(), $SwitchName, "1024","") ` | ProcessWMIJob $VirtualSwitchService "CreateSwitch").CreatedVirtualSwitch $ExternalNic = Get-WmiObject -Namespace "root\virtualization" -Class "Msvm_ExternalEthernetPort" ` -Filter "Name = '$PhysicalNICName'" $VirtualSwitchService.BindExternalEthernetPort($ExternalNic.__PATH) ` | ProcessWMIJob $VirtualSwitchService "BindExternalEthernetPort" $ExternalNicEndPoint = $ExternalNic.GetRelated("CIM_LanEndpoint") $ExternalSwitchPort = ($VirtualSwitchService.CreateSwitchPort($CreatedSwitch, ` [Guid]::NewGuid().ToString(), "ExternalSwitchPort", "") ` | ProcessWMIJob $VirtualSwitchService "CreateSwitchPort").CreatedSwitchPort $VirtualSwitchService.ConnectSwitchPort($ExternalSwitchPort, $ExternalNicEndPoint) ` | ProcessWMIJob $VirtualSwitchService "ConnectSwitchPort" }
The process is pretty straight forward – and even more so if you consider the Hyper-V networking model…
Taylor Brown Hyper-V Enterprise Deployment Team taylorb@microsoft.com http://blogs.msdn.com/taylorb