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.

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).

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.

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.

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.

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.

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.

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.