Useful vSphere PowerCLI scripts

I have been creating and using PowerCLI scripts for vSphere a lot again lately. And I thought it was a good idea to finally take all the snippets I collected and created over time and put them into one spot. The scripts in this post come in to play after the ESXi hosts are installed and the vCenter is up and running. If you are interested in deploying vCenter automatically you should check out my blog post about migrating from embedded to external PSC. In this post I will go over using the vcsa install tool. In here it is all PowerCLI, check out the scripts below.

  1. Create vCenter clusters
  2. Add ESXi hosts to vCenter clusters
  3. Create distributed switch and port groups
  4. Migrate hosts from vSwitch to distributed switch
  5. Create additional VM kernel ports
  6. Remove vSwitch0 from all hosts
  7. Create (sub) folders in vCenter
  8. DRS host groups
  9. Create and populate datastore clusters
  10. Change local ESXi host datastore names
  11. Supporting files
  12. Modules

As you can see, there are quite a few scripts here so this blog post will be longer then usual.

1. Create vCenter clusters

This script is used to create the initial datacenter object and multiple vCenter clusters. The script can configure all of the major settings  for HA and DRS. Some of the functions are not available in the latest version of vSphere PowerCLI. Things like Admission Control, Datastore Heartbeat, VMCP Settings or VM Monitoring are missing from the New-Cluster cmdlet. To enable this functionality I made use of some extra modules (more details below).

Back to index

2. Add ESXi hosts to vCenter clusters

After adding the required clusters it is time to bring the ESXi hosts in. This script will add ESXi hosts to vCenter based on a comma separated file. Hosts are placed in maintenance mode and added to the correct cluster. Finally the hosts are taken out of maintenance mode.

Back to index

3. Create Distributed switch and port groups

This script creates a single distributed switch and several port groups. The port group settings like VLAN, load balancing scheme and active uplinks are set as well.

Back to index

4. Migrate hosts from standard switch to distributed switch

This script is an important one and can save you a lot of hours. The process of moving multiple ESXi hosts from a standard switch to a distributed switch can be a time consuming task. This script will do it all for you based on a list of hosts. The base for this script came from William Lam who created this back in 2013.

Back to index

5. Create additional VM kernel ports

To create additional VM kernel ports for things like vMotion or Fault Tolerance Logging on all hosts you can use this PowerCLI script.

Back to index

6. Remove vSwitch0 from all hosts

After all ESXi hosts are migrated to a distributed switch you might want to remove the by default created vSwitch0. The following script removes that from all hosts in a specific vCenter.

Back to index

7. Create (sub) folders in vCenter

If you need to create a lot of folders in vCenter this script will come in handy. The top level scripts are created under the hidden ‘vm’ folder. Subfolders are created based on the top level folders that where created.

Back to index

8. DRS host groups

At the moment of writing this script uses cmdlets that are only available in the latest version of vSphere PowerCLI. I am using PowerCLI version 6.5.4 at this time. The two cmdlets used to create and edit DRS host groups need to be used together. The reason for this is that New-DrsClusterGroup can only add one host when creating the DRS host group. To add additional hosts the Set-DrsClusterGroup must be used to add the remaining hosts.

Back to index

9. Create and populate datastore clusters

This script speaks for it self. It will create datastore clusters and add data stores to it. It will do this based on a comma separated file.

Back to index

10. Change local datastore name on ESXI hosts

If you deploy a lot of ESXi hosts with local storage you will notice a lot if Datastore1 (x)’s in your environment. To rename them all to a more friendly name.

Back to index

11. Supporting files

In several scripts I am looping through a comma separated file, the formatting for these files are as follows.

clusters.txt

hosts.txt

DSClusters.txt

Back to index

12. Modules

The first script (create clusters) uses additional code to enhance the feature set of the script. This extra code comes in the form of modules.  Some of these modules I found on the internet because I was not familiar with this kind of scripting. I modified some of the code but left the original authors in the remarks where applicable, so all credits go to them. Module files have a specific extension, psm1.
They need to be  stored in \My Documents\WindowsPowerShell\Modules\<module_name>\<module_name.psm1>.

AdmissionConfig

This module lets you set admission control policy to a specific percentage.

 

DSHeartBeatConfig

If there is a need to configure data store heart beating, you can include this module.

 

VMCPSettings

Another feature that is not available in the default New-Cluster cmdlet is VCMP. This module can get and set the different settings for VCMP.

 

VMMonitoring

The final module I used can set the VM Monitoring settings.

Back to index

Conclusion

This has become a monster blog post because of all the scripts. Usually I write a conclusion at the end. For now I would like to say, hopefully these PowerCLI scripts are of use to you. Please feel free to change or enhance as you see fit. And please leave a comment if there is anything I could do to make these more efficient or better in general.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.