Upgrading ESXi environment with PowerCLI

A new ESXi 6.5 patch came out today:

https://kb.vmware.com/s/article/2151104

And I wanted to upgrade my whole lab environment to it and I haven’t set up auto-deploy or update manager yet (I plan to, making all of this much easier to manage). So I wrote a quick and dirty PowerCLI script that updates to the latest patch and if the host doesn’t have any VMs on it, puts it into maintenance mode and reboots it. I will reboot the other ones as needed.

So short, not really even worth throwing on GitHub, but I might make it cleaner, and smarter at some point and put it there.By the way, if you aren’t using Visual Studio Code for your PowerShell yet, I highly recommend it. Great for that and other languages–I have been working on some vRO stuff, and it has been helpful in authoring the JavaScript.

Some notes on this script:

  • Use at your own danger! It does reboot hosts, though only ones with no VMs. There is no error checking etc.
  • I am using the standard build, so if you have special build this won’t be the exact process.

Enjoy.

$esxihosts = get-vmhost
foreach ($esxi in $esxihosts)
{
    $esxcli = $esxi |get-esxcli -v2
    $argsInstall = $esxcli.software.profile.install.createargs()
    $argsInstall.depot = "https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml"
    $argsInstall.profile = "ESXi-6.5.0-20171204001-standard"
    $esxcli.software.profile.install.invoke($argsInstall)
    $vms = $esxi |get-vm
    if ($vms.count -eq 0)
    {
        $esxi | set-vmhost -State Maintenance
        $esxi | Restart-VMHost -RunAsync -Confirm:$false
    }
}

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.