There’s A Script for That

on December 23, 2015


If there’s a chance to save time by using scripts or automation tools, I always take it. Over the years I’ve found that there are a set of common themes associated with these scripts, and many that my colleagues and I use on a daily basis.

79651863_thumbnail

 

Here are some of the common categories that my scripts fall into:

Inventory/reporting

One of the most common use cases I’ve found for scripts is gathering data. This is especially useful when management asks for reports to validate licensing, or to check the state of multiple items in the environment. There are many occasions when you’re asked to find out how many VMs or hosts you have. How many VMs are running on “x” OS? Are VMware® tools up to date on all your VMs?

Here are some examples of scripts:

A list of VMware hosts, including their cluster and data center

get-vmhost | Select Name, @{N=”Cluster”;E={Get-Cluster -VMHost $_}},@{N=”Datacenter”;E={Get-Datacenter -VMHost $_}} | Export-csv c:\temp\inventory.csv

Check VMs that have memory limits set on them:

Get-VM | Get-VMResourceConfiguration | where {$_.MemlimitMB -ne -1}

If you want to target a specific cluster, just add Get-Cluster “clustername” to the beginning:

Get-Cluster “Clustername” | Get-VM | Get-VMResourceConfiguration | where {$_.MemlimitMB -ne -1}

Batch execution of actions

Sometimes you need to reconfigure many items. These could be vSphere™ objects like vSwitches, VMs or in guest configurations, such as changing all your Windows Servers® to make sure they are pointing to the correct DNS server.

Examples:

If we continue from our script above, checking if there were memory limits,

you could add the following if you wanted to remove the memory limits:
Set-VMResourceConfiguration -MemlimitMB $null

The final script would look like this:
Get-VM | Get-VMResourceConfiguration | where {$_.MemlimitMB -ne -1} | Set-VMResourceConfiguration -MemlimitMB $null

Health check

Look at health check scripts for your entire environment, instead of one-liners.

Alan Renouf’s vCheck: A classic script that’s only gotten better with time. It has since moved to Github. I schedule this on a weekly basis. It gives a great overview of the state of your vSphere environment. Having it emailed can give you a heads up on any potential issues in your environment.  It also allows for easy customization by the ability to write your own plugins, which is an excellent example of community effort that we all know and love in the virtual space. The same framework has also been used for reports on VSAN, Exchange™, and more.

Maintenance

The health check scripts I mentioned earlier are great, but what about automating maintenance? This is a great way to use the scripts you have for actions and execute them on a routine schedule. An example could be making sure HA and DRS are always enabled.

Here are some examples:

HA enablement:
Set-Cluster –Cluster “ClusterNameHere” –HAEnable $true

HA enablement and setting HA admission control to tolerate 1 host failure
Set-Cluster –Cluster “ClusterNameHere” –HAAdmissionControlEnabled $true –HAFailoverLevel 1

DRS enablement:
 Set-Cluster –Cluster “ClusterNameHere” –DRSAutomationLevel FullyAutomated –Confirm:$false

Decide the standards for your cluster and then have the DRS and HA script run on a schedule. It’s a good idea to run it every night at midnight, to make sure that if someone forgets to switch it back from partially automated to fully automated, you know the script is going to take care of it. Using other tools, like vRealize Orchestrator, in conjunction with these scripts helps tremendously. You could add additional logic if you are expecting specific clusters to have different settings and accommodate change control windows. Again, decide on your business logic and adjust the script to match.

Code repository

This leads me to a much newer community initiative.  A github script repository  by VMware that should become the go-to place if you’re looking for a PowerCLI script. I haven’t seen too much out on this, but there are many iterations of practically the same script online. This is a chance to have a central location where community members contribute their scripts.  Check it out and submit your work!

This post was originally published on the SolarWinds IT Resource Center.

Related Posts