Update Host DCUI with PowerCLI

With randomly generated host names keeping track of what host you’re working on can get difficult. Recently I was in the process of performing host name changes along with cluster migrations/rebalancing which made it even more difficult to keep track of what I was working.

So, of course, I decided to write a script that will update the DCUI of a host.

Normally this is what we see in the DCUI:

We can see the name, IP address, processors, RAM, version and build. A decent amount of info, but I wanted cluster, powered on VM count, and even the serial number of the server.

Now for the script itself.

First we need a list of all the hosts that are currently available. No need to try to update a host that is not currently responding:

$allHosts = Get-VMHost | Where {$_.ConnectionState -ne "NotResponding"}

Now we open a ForEach loop so these commands can be run against each of these hosts:

ForEach ($vmhost in $allHosts) {

First we’ll get a count of powered on VMs on the host:

$vmCount = ($vmhost | Get-VM | Where {$_.PowerState -eq "PoweredOn"}).Count

Now we’ll get the cluster name:

$cluster = $vmhost.Parent.Name

Then we capture the make and model of the host:

$make = "$($vmhost.Manufacturer) $($vmhost.model)"

Let’s capture the number of CPUs:

$cpuCount = $vmhost.ExtensionData.Summary.Hardware.NumCpuPkgs

Then we’ll get the model of the CPUs:

$cpuModel = $vmhost.ExtensionData.Summary.Hardware.CpuModel

And let’s get the amount of RAM in GB rounded to 2 decimal places:

$memory = [math]::round($vmhost.MemoryTotalGB,2)

This is where create our DCUI text and use the variables we’ve captured above:

$DCUIString = "{color:white}
		{esxproduct} (VMKernel Release {esxversion})
		
		$make
		
		$cpuCount x $cpuModel
		$memory GB Memory
		
		ServiceTag: {servicetag}
		{/color}
		
		{color:yellow}
		Hostname: {hostname}
		IP Address: {ip}
		Cluster: $cluster
		Powered On VMs: $vmCount
		{/color}
		
		
		
		
		<F2> Customize System/View Logs
		<F12> Shut Down/Restart
		"

Then we apply these settings to the host and close the ForEach loop:

$vmhost | Set-VMHostAdvancedConfiguration -Name Annotations.WelcomeMessage -Value $DCUIString }

The script will loop through all your hosts and now you’ll see a DCUI that looks like this:

I have this script scheduled to run daily so it is mostly up to date. In addition, I run another version every 15 minutes. In the event that a host has been placed in maintenance mode we update only that host so we can see if any powered on VMs reside on it.

For that to work, replace the first part of the script searching for all hosts with the part below:

$allHosts = Get-VMhost | where {$_.connectionstate -eq "Maintenance"}

Full script below:

$allHosts = Get-VMhost | where {$_.connectionstate -ne "NotResponding"}
ForEach ($vmhost in $allHosts) {
$vmCount = ($vmhost | Get-VM | where {$_.powerstate -eq "poweredon"}).count
$cluster = $vmhost.parent.name
$make = "$($vmhost.Manufacturer) $($vmhost.model)"
$cpuCount = $vmhost.ExtensionData.Summary.Hardware.NumCpuPkgs
$cpuModel = $vmhost.ExtensionData.Summary.Hardware.CpuModel
$memory = [math]::round($vmhost.MemoryTotalGB,2)
$DCUIString = "{color:white}
		{esxproduct} (VMKernel Release {esxversion})
		
		$make
		
		$cpuCount x $cpuModel
		$memory GB Memory
		
		ServiceTag: {servicetag}
		{/color}
		
		{color:yellow}
		Hostname: {hostname}
		IP Address: {ip}
		Cluster: $cluster
		Powered On VMs: $vmCount
		{/color}
		
		
		
		
		<F2> Customize System/View Logs
		<F12> Shut Down/Restart
		"
$vmhost | Set-VMHostAdvancedConfiguration -Name Annotations.WelcomeMessage -Value $DCUIString}