I have recently been doing a large amount of work with host profiles and PowerCLI as part of an automation project to streamline our process of adding new hosts in to our VMware environment, as  a result I am going to be writing up a few posts about some things that I had to overcome to get it all working properly, as Host Profiles are not covered extensively by the standard cmdlets.  The first task I am going to write about is one of the simpler parts, updating a host profile from the profiles reference host.

First of all we need to get the host profile we want to work with.

$Profile = Get-VMHostProfile -Name $HostProfile

Now that we have the profile information loaded in to our session we can use the vSphere API to create a Host Profile Config Spec based on the reference host of the host profile.

$spec = New-Object VMware.Vim.HostProfileHostBasedConfigSpec
$spec.Host = New-Object VMware.Vim.ManagedObjectReference
$Spec.Host = $Profile.ReferenceHost.ExtensionData.MoRef
$Spec.useHostProfileEngine = $true

The first line here is creating the $Spec variable as a new Host Basee Config Spec object, then the following lines are specifying that the spec should be based on the reference host of the Host Profile specified earlier. What we have now is the original Host Profile, and an Update Spec which is essentially an Object that contains all the configuration details from the reference host which we will use to update the original host profile.

Finally we can call the update method on the original host profile to copy the data from the Update Spec to the Host Profile and save it back to vCenter.

$Profile.ExtensionData.UpdateHostProfile($Spec)

The reult of the above will now be the original host profile will be updated from the reference host the same as if the task had been run from the GUI clinet, but this can easily be built in to automation scripts to ensure new hosts are being added to the cluster with the latest configuration.

The Complete Script is

#Get Existing Host Profile
$Profile = Get-VMHostProfile -Name $HostProfile

#Create the update Spec
$Spec = New-Object VMware.Vim.HostProfileHostBasedConfigSpec
$Spec.Host = New-Object VMware.Vim.ManagedObjectReference
$Spec.Host = $Profile.ReferenceHost.ExtensionData.MoRef
$Spec.useHostProfileEngine = $true

#Update the host Profile
$Profile.ExtensionData.UpdateHostProfile($Spec)

Comments