PowerCLI script for applying Host Profile

Encountered an issue with Auto Deploy this week where it wasn’t properly assigning the VMware License key contained within the Host Profile, so the ESXi hosts would not add themselves into the vCenter environment automatically. Looking into the issue, we only have this problem when we try to have Auto Deploy add in HP blade servers using the VSAN license key for this environment.

Anyway, it became necessary to add the hosts in manually after they each PXE booted into ESXi and apply the proper license key. From there, because we are doing everything through the slower web client for VSAN, we were going to have to apply the host profile to 80 hosts and there was no way I wanted to do that manually.

So I wrote up a quick script to handle this for us. Feel free to reuse if you find it helpful.

[string]$VCENTER = ""
[string]$USER = "administrator@vsphere.local"
[string]$PASSWD = ""
[string]$HOSTPROFILE = "vCache"

Connect-VIServer $VCENTER -User $USER -Password $PASSWORD

$i = 100
while ($i -le 120) {
  $hostname = "mraesxi" + $i " ".local.domain"
  Set-VMHost -VMHost $hostname -State "Maintenance" -Confirm:$false
  Apply-VMHostProfile -Entity $hostname -Profile $HOSTPROFILE -Confirm:$false
  Set-VMHost -VMHost $hostname -State "Connected" -Confirm:$false
  $i++
}

The script itself could likely be more efficient, especially around putting everything into maintenance mode and then apply the host profile to all the virtual machines in a parallel fashion. This was a quick hack to get around an issue we were dealing with while trying to bring online a bunch of blade chassis’ at once.