Use PowerCLI to upgrade vSphere

So today marked the release of vSphere 6.0 U2. This is pretty cool because it adds the vSphere Host Web Client. A much-needed upgrade if you speak to administrators of a vSphere environment. I have a small lab of around 16 hosts. It forms my miniature lab that allows me to test and scale NSX deployments and use-cases. I am in the midst of writing the book and wanted to minimise the time taken to update. This environment does not use VMware Update Manager so I have dallied with my new love, PowerCLI.

The following script does not consider or take into consideration the workloads. My management cluster along with test applications mean nothing to me.  If you are to use this then do not use -force or -confirm:$false. Do not stop VM’s either – let vMotion and DRS do the work!

So after downloading the latest vSphere update ZIP to the cluster data store the following code is run.

$datastorename = "mgt-ds01"
$clustername = "Management"

$hosts = get-cluster $ClusterName | get-vmhost

$hosts | get-vm | stop-vm -confirm:$false
$hosts | set-vmhost -state "maintenance" -confirm:$false
$hosts | Install-VMHostPatch -HostPath /vmfs/volumes/$datastorename/60u2/
$hosts | restart-vmhost -force -confirm:$false
$hosts | set-vmhost -state "Connected" -confirm:$false
$hosts | get-vm | start-vm

The two environment variables outline the cluster datastore and the cluster name. The order of operations are as follows:

  1. Return all host within defined cluster and store it in $hosts
  2. Get all VMs across all hosts in cluster and hard stop without confirmation
  3. Set all hosts within cluster to go into maintenance mode
  4. Install the host patch from the datastore location defined
  5. Force restart all hosts
  6. Reconnect hosts out of maintenance mode
  7. Restart VMs on hosts within cluster

Screenshot 2016-03-16 23.13.13

Get the latest info on vSphere 6.0 here

vSphere 6.0 U2 Release Notes:

1 thought on “Use PowerCLI to upgrade vSphere”

Leave a Reply

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