Add PortGroup to all hosts in cluster with PowerCLI
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Add PortGroup to all hosts in cluster with PowerCLI相关的知识,希望对你有一定的参考价值。
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 standard vSwitches is that there are as many of them as there are ESX hosts and each must be separately created and managed. Add to that, multiple port groups on multiple uplinks and you have a growing administrative challenge.
While we don’t make these types of changes often, it is a change that can take a significant effort. More importantly, it is prone to human error.
Enter PowerCLI
With PowerCLI we have lots of great options. Because our hosts are in a cluster, the process flow will be fairly simple:
Query a file with our VLAN information
Query our vSphere cluster for hosts
Add vSwitch port groups to each host in each cluster
Three easy steps, but the amount of clicks and typing to manually configure all of these port groups would be a real challenge. With a script we ensure consistency of the process.
Your Input File
We are working with a file that is named MyVLANs.csv and is located somewhere that 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 is organized as shown in our example here:
As we can see, we name the cluster, the vSwitch, the name to apply to the port group (VLANname) and the VLAN number to assign to that port group.
In your environment you could have numerous clusters, and you can use the same source file to manage additions to all of them at the same time.
The Script
I did say it was a three-step process, but one of those steps has a few more activities to perform. That being said, it still isn’t too much to do thanks to PowerCLI.
We assume that you are 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 have to parse the contents. Because we have our header row, the columns are 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 is 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 CmdLet:
ForEach ($VMHost in $MyVMHosts) {
Get-VirtualSwitch -VMHost $VMHost -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid
}
Here is the view of the whole script, and here is the link to the file: http://www.discoposse.com/wp-content/uploads/AddVLANsToClusterHosts.ps1
About the Errors
One of the things I haven’t done with this script is 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 on an existing set of port groups it will throw an error because the port group exists. If I get some extra time I may add a little error housekeeping to clean things up.
Hope this saves you some time. Happy scripting!
以上是关于Add PortGroup to all hosts in cluster with PowerCLI的主要内容,如果未能解决你的问题,请参考以下文章
Export portgroup from the host
Copy vSwith portgroup from an existing ESXi host
spark启动失败:localhost failed to add the host to the list of known hosts
spark启动失败:localhost failed to add the host to the list of known hosts
How to Add Linux Host to Nagios Monitoring Server Using NRPE Plugin
Python script to create Screen from all Items/Graphs of a host