Jorge's Quest For Knowledge!

All About Identity And Security On-Premises And In The Cloud – It's Just Like An Addiction, The More You Have, The More You Want To Have!

(2012-10-09) Configuring A Network Switch In Windows Server 2008 R2 Hyper-V With PowerShell

Posted by Jorge on 2012-10-09

For a project at my customer I was creating a PowerShell script to fully automate the installation and configuration of Hyper-V that was going to be used for security sensitive virtual machines. One of the steps was configuring the network switches within Hyper-V. To achieve that I searched the internet for snippets that used native CMDlets.

This is how it looks like:

$localHyperVHostName = $ENV:COMPUTERNAME $virtualSwitchMgmtSvc = Get-WMIObject Msvm_VirtualSwitchManagementService -namespace "root\virtualization" -computername $localHyperVHostName $physicalNetworkAdapters = Get-WMIObject Msvm_ExternalEthernetPort -namespace "root\virtualization" -computername $localHyperVHostName | ?{$_.IsBound -eq $False -And $_.EnabledState -eq "2"} $ExternalEthernetPortNameVMsLAN = Get-WMIObject win32_networkadapter | ?{$_.NetConnectionID -eq "VMs LAN"} | %{$_.Name} $ExternalEthernetPortVMsLAN = $physicalNetworkAdapters | ?{$_.Name -eq $ExternalEthernetPortNameVMsLAN} $InternalEthernetPortFriendlyNameVMsLAN = "VM Guests Network" $InternalSwitchPortFriendlyName = "InternalSwitchPort" $ExternalSwitchPortFriendlyName = "ExternalSwitchPort" $switchGuidVMsLAN = [guid]::NewGuid().ToString() $InternalSwitchPortGuidVMsLAN = [guid]::NewGuid().ToString() $ExternalSwitchPortGuidVMsLAN = [guid]::NewGuid().ToString() $InternalEthernetPortGuidVMsLAN = [guid]::NewGuid().ToString() $resultCreateSwitchVMsLAN = $virtualSwitchMgmtSvc.CreateSwitch($switchGuidVMsLAN, $InternalEthernetPortFriendlyNameVMsLAN, "1024", $null) $switchVMsLAN = $resultCreateSwitchVMsLAN.CreatedVirtualSwitch $resultCreateInternalSwitchPortVMsLAN = $virtualSwitchMgmtSvc.CreateSwitchPort($switchVMsLAN, $InternalSwitchPortGuidVMsLAN, $InternalSwitchPortFriendlyName, $null) $switchPortInternalVMsLAN = $resultCreateInternalSwitchPortVMsLAN.CreatedSwitchPort $resultCreateExternalSwitchPortVMsLAN = $virtualSwitchMgmtSvc.CreateSwitchPort($switchVMsLAN, $ExternalSwitchPortGuidVMsLAN, $ExternalSwitchPortFriendlyName, $null) $switchPortExternalVMsLAN = $resultCreateExternalSwitchPortVMsLAN.CreatedSwitchPort $resultSetupSwitchVMsLAN = $virtualSwitchMgmtSvc.SetupSwitch($switchPortExternalVMsLAN, $switchPortInternalVMsLAN, $ExternalEthernetPortVMsLAN, $InternalEthernetPortGuidVMsLAN, $InternalEthernetPortFriendlyNameVMsLAN) Start-Sleep -s 30 # This is needed to that Hyper-V has the time to finish the creation of the switch $InternalEthernetPortVMsLAN = Get-WMIObject Msvm_InternalEthernetPort -namespace "root\virtualization" -computername $localHyperVHostName | ?{$_.ElementName -eq $InternalEthernetPortFriendlyNameVMsLAN} $virtualSwitchMgmtSvc.DeleteInternalEthernetPort($InternalEthernetPortVMsLAN) | Out-Null

After being almost finished, I found the Hyper-V PoSH module on Codeplex created by a few Microsoft engineers. I also decided to achieve the same result, but instead using these new CMDlets.

This is how it looks like:

Import-Module HYPERV $localHyperVHostName = $ENV:COMPUTERNAME $SwitchNameVMsLAN = "VM Guests Network" $nicVMsLAN = Get-WMIObject win32_networkadapter | ?{$_.NetConnectionID -eq "VMs LAN"} | %{$_.Name} New-VMExternalSwitch -VirtualSwitchName $SwitchNameVMsLAN -ExternalEthernet $nicVMsLAN -Server $localHyperVHostName -Force Remove-VMSwitchNIC $SwitchNameVMsLAN -Server $localHyperVHostName -Force

See the difference? That just rocks!

I decided to continue and use the Hyper-V PoSH module as that was why easier to get stuff done!

By the way, if you want to install the Hyper-V PoSH module, don’t use the installation steps provided, but rather extract the ZIP file and move the HYPERV folder and its contents to the “C:\Windows\system32\WindowsPowerShell\v1.0\Modules\” folder.




* This posting is provided "AS IS" with no warranties and confers no rights!

* Always evaluate/test yourself before using/implementing this!



############### Jorge’s Quest For Knowledge #############

######### ########



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: