PowerCLI: Easy vSwitch & PortGroup Setup
To get VMotion working the networking setup plays a big part, any of the following could cause you big issues:
- A spelling mistake in a PortGroup name
- A missing PortGroup
- A PortGroup configured incorrectly
- A non-existing PortGroup on one of your hosts
- etc
This is why automation and PowerCLI is key to the setup of your vSwitches and PortGroup’s, you could add each vSwitch manually and then create a PortGroup for each VLAN you have trunked to your hosts manually but what are the chances you will miss one, what are the chances you will get the VLAN ID incorrect ? And you sure do get bored of the wizard when you have 4 vSwitches and around 20 PortGroups !
The following code will take you through copying all vSwitches and PortGroups from an existing ESX server over to a new server, ensuring they are exactly the same. It sure does save me time !
$VISRV = Connect-VIServer (Read-Host "Please enter the name of your VI SERVER")
$BASEHost = Get-VMHost -Name (Read-Host "Please enter the name of your existing server as seen in the VI Client:")
$NEWHost = Get-VMHost -Name (Read-Host "Please enter the name of the server to configure as seen in the VI Client:")
$BASEHost |Get-VirtualSwitch |Foreach {
If (($NEWHost |Get-VirtualSwitch -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
Write-Host "Creating Virtual Switch $($_.Name)"
$NewSwitch = $NEWHost |New-VirtualSwitch -Name $_.Name-NumPorts $_.NumPorts-Mtu $_.Mtu
$vSwitch = $_
}
$_ |Get-VirtualPortGroup |Foreach {
If (($NEWHost |Get-VirtualPortGroup -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
Write-Host "Creating Portgroup $($_.Name)"
$NewPortGroup = $NEWHost |Get-VirtualSwitch -Name $vSwitch |New-VirtualPortGroup -Name $_.Name-VLanId $_.VLanID
}
}
}







about 8 months ago
Al, i had to put “$vSwitch = $_” after “$BASEHost |Get-VirtualSwitch |Foreach {” in case of only vSwitch0
about 6 months ago
Has anyone (else) tried this on ESX4? I keep getting this error
“The argument cannot be null or empty.
At :line:30 char:60
+ $NewPortGroup = $NEWHost |Get-VirtualSwitch -Name <<<< $vSwitch |New-VirtualPortGroup -Name $_.Name-VLanId $_.VLanID"
Line 30 equates to "$NewPortGroup = $NEWHost……." on my script.
about 6 months ago
@Kcm
I havent tried it against ESX4 yet but will now !
about 1 month ago
I had to modify the script a tiny bit (sounds like the above comment #1) to get it to work against ESX 4, but below is my version (with all thanks to Al!). I did get this to work for my vSphere hosts.
$VISRV = Connect-VIServer (Read-Host “Please enter the name of your VI SERVER”)
$BASEHost = Get-VMHost -Name (Read-Host “Please enter the name of your existing server as seen in the VI Client:”)
$NEWHost = Get-VMHost -Name (Read-Host “Please enter the name of the server to configure as seen in the VI Client:”)
$BASEHost |Get-VirtualSwitch |Foreach {
$switch = $_.Name
If (($NEWHost |Get-VirtualSwitch -Name $switch -ErrorAction SilentlyContinue)-eq $null){
Write-Host “Creating Virtual Switch $($_.Name)”
$NewSwitch = $NEWHost |New-VirtualSwitch -Name $_.Name-NumPorts $_.NumPorts-Mtu $_.Mtu
#$vSwitch = $_
}
$BASEHost | Get-VirtualPortGroup -VirtualSwitch $switch | Foreach {
$myPG = $_.name
If (($NEWHost |Get-VirtualPortGroup -Name $myPG -ErrorAction SilentlyContinue)-eq $null){
Write-Host “Creating Portgroup $($_.Name)”
$NewPortGroup = $NEWHost |Get-VirtualSwitch -Name $vSwitch |New-VirtualPortGroup -Name $_.Name-VLanId $_.VLanID
}
}
}
about 1 month ago
Hi Al/Mike,
I’m not very experienced with the PowerCLI, but am very interested in automating the creation of vSwitches and PortGroups. We are using ESX4.
I tried pasting Mike’s script in the PowerCLI, but keep getting this error:
Get-VirtualPortGroup : 28-1-2010 17:02:38 Get-VirtualPortGroup
Could not find VirtualSwitch with name ‘vSwitch0′.
At line:8 char:33
+ $BASEHost | Get-VirtualPortGroup <<<< -VirtualSwitch $switch | Foreach {
+ CategoryInfo : ObjectNotFound: (vSwitch0:String) [Get-VirtualPortGroup], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.Commands.GetVirtualPortGroup
I even do not get the questions for vCenter server, source and destination server. I'm running the PowerCLI with my vmWare admin account (not root)
Any suggestions?
about 1 month ago
Hmm.. not sure M@rcel. Double-check the way this gets pasted in to PowerCLI perhaps. Watch that line breaks don’t get put in where they shouldn’t be.
Perhaps copy “my” version of the script and paste into notepad or some other text editor and watch for the line breaks. Have you tried to use PowerGUI and its Script Editor at all?
Mike
about 1 month ago
@Mike, strange, all works fine with my original script on ESX4 here. I will look through the differences.
about 1 month ago
My only real change was to use a variable in place of $_ in the foreach loops. For some reason, it didn’t seem to get the scope of $_ without assigning it to a variable first and then using the variable ($switch or &myPG in my version).
I learned a lot from looking at your script and tweaking it, Al (even if the tweaks turn out to have been unnecessary). Thanks!
about 1 month ago
No problems, Im just glad its not just me using them