Automate vSwitch and VMkernel configuration VMware PowerCLI

0

As we have already detailed in this post about how we can configure VMware clusters with PowerCLI and the benefits of doing so, the sky is the limit in what we can do with VMware PowerCLI and automating configuration processes on our ESXi hosts including our network configuration.  One of the most tedious and labor intensive configuration processes especially with the Web client is configuring vSwitches and VMkernel ports especially if using Standard vSwitches.  In this quick Friday post, I wanted to paste over some PowerCLI code to configure a couple of vSwitches – one for iSCSI and one for vMotion.  Today’s topic – Automate vSwitch and VMkernel configuration VMware PowerCLI.

Automate vSwitch and VMkernel configuration VMware PowerCLI

The first thing we need to do is setup a CSV file that will hold all the values that we will be passing into our PowerCLI script.  Below is the example CSV file that you can use as a template.  You will of course need to change the iSCSI and vMotion IP addresses and subnets defined along with which vmnics you want to assign to which vSwitch.

Now for the PowerCLI code that will utilize the values we have defined in our CSV file.  You will need to populate the path to your CSV file that you want to use.  Also, there are no checks in the script below to verify connection to a vCenter server, so you will want to make sure to use the connect-viserver commandlet and connect to vCenter beforehand.  The values are defined based on the CSV import.  Also, there are checks in place in the script to check and see if the vSwitches as well as the VMkernel ports already exist.  If they do, the script will not attempt to create any of the vSwitches or the VMkernel ports.

The Code

The process will complete in under a minute on a couple of hosts and makes it extremely painless compared to the Web client to get your VSS switches configured and VMkernel ports created and configured with IP addresses, MTU values, and appropriate vmnics defined.

Thoughts

The more you use PowerCLI to complete labor intensive tasks such as creating VSS switches and port groups, the more you will grow to love it and use it.  This quick script to Automate vSwitch and VMkernel configuration VMware PowerCLI process shows just how easy it is to create a CSV file and then use the input to configure our switches and VMkernel ports.