Virtually everything is poshable
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
}
}
}
| Print article | This entry was posted by Virtu-Al on June 27, 2009 at 18:59, and is filed under powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







(3)
(13)
(0)
about 1 year ago
Al, i had to put “$vSwitch = $_” after “$BASEHost |Get-VirtualSwitch |Foreach {” in case of only vSwitch0
about 11 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 11 months ago
@Kcm
I havent tried it against ESX4 yet but will now !
about 7 months 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 7 months 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 7 months 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 7 months ago
@Mike, strange, all works fine with my original script on ESX4 here. I will look through the differences.
about 7 months 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 7 months ago
No problems, Im just glad its not just me using them
about 1 month ago
Does this script work with ESXi? I get the above Null or Empty error when running against ESX4i, but it works great running against ESX4.
about 1 month ago
I used it just the other day against an ESXi 4 host, but not without some issues. I was using a version I am sorta working on that tries to create the vswitches, add portgroups and VMkernel ports and adds vnics to the vswitches. It does this by looking at an existing host and making the new host just like the old one. Still not much of my work – mainly Al’s.
The part ESXi didn’t seem to like was the fact that the VMkernel ports are vmk0, vmk1, etc. instead of looking a bit like ESX portgroups. Still things to look at in “my” version before it is ready to use in Production.