Working with host profiles in the vSphere PowerCLI

After learning how to create and manipulate host profiles from the vSphere PowerCLI, you can check and correct host compliance with a few short commands.

VMware host profiles can assist with automation, repeatability and tight configuration management, all of which...

are disciplines that can help improve the maturity of an IT shop. Taking advantage of features like host profiles become increasingly important as a virtual infrastructure grows.

In this article, I'll explain how to use the vSphere PowerCLI with host profiles to provision new hosts and check existing hosts against set compliance policies.

Note: you must have PowerCLI 4.0 or greater, vSphere 4.0 and the correct licenses to be able to use VMware host profiles. To verify which version you are running, type Get-PowerCLIVersion at a PowerCLI prompt.

Host profile cmdlets

First and foremost, we need to know which cmdlets are available for working with host profiles. The quickest way to find out is to type Get-Help at the vSphere PowerCLI command prompt. If you add a search string like "*hostprofile*" you'll see a screen like this:

The screenshot omits the full cmdlet synopsis, but you can run Get-Help followed by the name of a single cmdlet to find out more about what each cmdlet does.

If you understand how host profiles work in the vSphere Client, then the purpose of the relevant cmdlets should be self-explanatory. For the rest of us: The concept behind a host profile is that you select a host server to be your "reference host." When you use host profiles, hosts created on the reference host will have the same settings as the reference host.

Step one is to create a host profile based on your reference host, which is done in the vSphere client.

Alternatively, you can use the New-VMHostProfile cmdlet, which I'll show you later. Once you have a host profile, you can then attach it to a cluster or individual hosts. After doing so, you are able to check the compliance of one or more hosts or an entire cluster against the settings contained within the profile.

In PowerCLI, you can perform the attachment with the Apply-VMHostProfile cmdlet and the compliance testing with the Test-VMHostProfileCompliance cmdlet.

The last and most significant task involved with host profiles is applying the profile settings to a new host. This step is where you effect any configuration changes to the destination host systems. Obviously, changing major configuration settings on a host server can interrupt its ability to provide virtualization services. For that reason, a host has to be in maintenance mode in order for you to apply settings to it. When you are ready to trigger the necessary configuration changes you will use the Apply-VMHostProfile cmdlet a second time.

Creating a new host profile with PowerCLI

Creating host profiles is easy to do with PowerCLI. You just supply a name and the reference host, as can be seen in the image below.

Simple, right? The operation only took a few seconds. The real magic happens later, when you start applying these settings across your clusters. Here is a screenshot from the vSphere Client showing some of the settings contained within my new profile:

Now let's move on to the next step: attaching hosts.

Attaching hosts and clusters to a host profile

As I mentioned earlier, the actions of attaching a host and applying the settings are performed in PowerCLI with a single cmdlet: Apply-VMHostProfile. Before getting to an example, let's examine the parameters for this cmdlet.

Name Description
Entity The inventory item (host or cluster) to be affected.
Profile The name or object of the host profile to be applied.
Variable Accepts a hashtable containing profile variables, such as a vSwitch IP address.
AssociateOnly Set this flag to attach a profile to an entity without applying it.
ApplyOnly Set this flag to apply a profile to an entity without attaching it.
RunAsync Set this flag to immediately return control of your script, while vCenter performs specified actions in the background.
Server Optionally specify one or more vCenter server connections. If you don't specify a server, then the action with apply to all active vCenter connections.

Note: There are some caveats when using Apply-VMHostProfile. Be sure to check out the help section in PowerCLI for more information.

Let's go ahead and associate a profile to a cluster:

If successful, the input object (the entity) will be returned back to you. With the current version of the Get-VMHostProfile cmdlet, you can't easily see which entities are attached, but I'll show you how to get at that level of detail using the Get-View and Get-VIObjectByVIView cmdlets.

Checking host profile compliance

Let's take the host profile stored in the $profile variable and check for compliance. You do that with the Test-VMHostProfileCompliance cmdlet.

The test cmdlet can return a lot of information, so I recommend assigning the results to a variable. Here's how you do that:

On the second line, I "piped" the $test variable to the Format-List cmdlet. I did this because some of the information was truncated when it was displayed in the default table view. The really interesting stuff comes out when you examine the IncomplianceElementList property of the $test variable. At the bottom of this screenshot, you can see in plain English the cause for the compliance check to fail. This can be a great tool for checking configuration drift in your vSphere environment.

Check out what I see when using the vSphere Client:

Applying host profile settings

Let's get the rogue ESX host into compliance. As mentioned above, the host must first be in maintenance mode.You can do this via PowerCLI by using the Set-VMHost and State parameter cmdlets. You need to be prepared to take a host out of a cluster before doing this, just as you would if you were using the vSphere client.

In the second command I applied the profile. In other words, vSphere created the missing portgroup. Let's check the compliance again:

If you click Refresh in the vSphere Client, you'll see that everything is "green" at this point.

After this tutorial you should have a good handle on what you can do with host profiles from PowerCLI. Now get out there and test for compliance!

Hal Rottenberg is a Microsoft PowerShell MVP and VMware vExpert living in Woodstock, Georgia. He is well-known in system administrator circles for co-hosting the PowerScripting Podcast and heading up the PowerShellCommunity organization. He is also the author of a book titled "Managing VMware Infrastructure with PowerShell: TFM" published by SAPIEN Press in 2009.

This was last published in July 2010

Dig Deeper on Scripting administrative tasks

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.