http://discoposse.com/2013/02/14/powercli-add-multiple-vlan-port-groups-to-vsphere-cluster-standard-switches/Limitations of standard Vswitches
One of the negative points of the standard vswitches was that there was as many of them as there was ESX hosts and each must be Separately created and managed. Add to this, multiple port groups on multiple uplinks and you have a growing administrative challenge.
While we don't make these types of changes often, it's a change that can take a significant effort. More importantly, it's prone to human error.
Enter POWERCLI
With Powercli we have lots of great options. Because our hosts is in a cluster, the process flow would be fairly simple:
Query a file with our VLAN information
Query our VSphere cluster for hosts
ADD VSwitch port groups to each host cluster
Three easy steps, but the amount of clicks and typing to manually configure all of these ports groups would be a real chall Enge. With a script we ensure consistency of the process.
Your Input File
We are working with a file, named Myvlans.csv and is located somewhere, the script can read it from as defined In the script itself. We assign a variable $InputFile and use the full path to find it in our script file.
The file has a header row and the data are organized as shown in we example here:
650) this.width=650; "Width=" 491 "height=" 267 "class=" AlignCenter size-full wp-image-2085 "style=" border:0px; "alt=" Inputfile "src=" Http://cdn.discoposse.com/wp-content/uploads/2013/02/inputfile.png "/>
As we can see, we name the cluster, the VSwitch, the name to apply to the port group (vlanname) and the VLAN number to the IGN to that Port group.
In your environment you could has numerous clusters, and you can use the same source file to manage additions to all of t Hem at the same time.
The Script
I did say it is a three-step process, but one of the those steps have a few more activities to perform. That's being said, it still isn ' t too much to does thanks to POWERCLI.
We assume that is running your powercli shell with privileges to administer your vCenter environment. This requires access to read the file and to add port groups to the VSphere vswitches.
First we setup our input file:
$InputFile = "C:usersewrightdocumentsscripts-tempmyvlans.csv"
Next We import the file using the handy import-csv CmdLet:
$MyVLANFile = Import-csv $InputFile
Now we just has to parse the contents. Because we have our header row, the columns is already assigned and we just loop through each line using ForEach To read the info, create our POWERCLI command to add the VSwitch and execute.
Because we have to read the cluster information for each line there are another loop inside to add the VSwitch to each host for the cluster.
ForEach ($VLAN in $MyVLANFile) {
$MyCluster = $VLAN. Cluster
$MyvSwitch = $VLAN. VSwitch
$MyVLANname = $VLAN. Vlanname
$MyVLANid = $VLAN. Vlanid
We define variables for each column in the file, then query the cluster for hosts and assign it to the $MyVMHosts Variable
$MyVMHosts = Get-cluster $MyCluster | Get-vmhost | Sort Name | % {$_. Name}
Next we loop through each host, query the VSwitch and create a new port group with the New-virtualportgroup cmdle T:
ForEach ($VMHost in $MyVMHosts) {
Get-virtualswitch-vmhost $VMHost-name $MyvSwitch | New-virtualportgroup-name $MyVLANname-vlanid $MyVLANid
}
Here are the view of the whole script, and here are the link to the FILE:HTTP://WWW.DISCOPOSSE.COM/WP-CONTENT/UPLOADS/ADDVL Anstoclusterhosts.ps1
650) this.width=650; "Width=" 665 "height=" 369 "class=" AlignCenter wp-image-2086 "style=" border:0px; "alt=" ScriptFile " src= "Http://cdn.discoposse.com/wp-content/uploads/2013/02/scriptfile.png"/>
About the Errors
One of the things I haven ' t-do with this script was any error handling. Because I ' m only adding switches on occasion it doesn ' t need a lot of cleanliness. If you re-run the same file in an existing set of port groups it would throw an error because the port group exists. If I get some extra time I may add a little the error housekeeping to clean things up.
Hope This saves you some time. Happy scripting!
Add PortGroup to all hosts in cluster with POWERCLI