Have you ever needed to move\restore Standard vSwitch portgroups? If so, I’m hoping you didn’t do it manually. o_0. I recently had to re-install a few servers that used the Standard vSwitch, and I cobbled together some PowerCLI code to help with the transition. Code samples included after the jump…
Standard disclaimers apply: Source code provided for instructional purposes only without any guarantee of support, maintenance, or liability. Use at your own risk!
Step 1: Get the name of your Standard vSwitch
get-virtualswitch -vmhost (get-vmhost -name "host01.vmtrooper.com") -Standard
This command will get all of the standard vSwitches on the server in question. You can then save the vSwitch object you are interested in to a variable using the “-Name” switch:
$vSwitch = get-virtualswitch -vmhost (get-vmhost -name "host01.vmtrooper.com") -Standard -Name vSwitch0
Step 2: Get the list of the portgroups that you need to recreate:
Get-virtualportgroup -VirtualSwitch $vSwitch
Step 3: Write PowerCLI code to recreate the portgroups on the newly installed server:
New-VirtualPortGroup -VirtualSwitch $vSwitch -Name Portgroup01 -VLanId 27 New-VirtualPortGroup -VirtualSwitch $vSwitch -Name Portgroup02 -VLanId 28 . . .
Of course, the more elegant solution would be to use a while\for loop to iterate through the list of portgroups obtained from Step 2. In a future update, I may post version 2 of the Standard vSwitch Portgroup script that includes arrays, loops, etc.
Here are a few tools & resources that I recommend for getting up to speed on PowerCLI