This is the third and final post in this little mini series on working with Host Profiles with PowerCLI, which will cover the process of supply answers to the questions provided by host profiles and then finally applying the profile to the required host.

The first part of this is to attempt to apply the host profile to a host, it will fail to apply the profile but will return a PowerShell object with all the questions which will require an answer before the profile can be applied proper.  We can store the output from this command in the variable $Questions we will also initialize a hash table to pair up the questions with the answers.

$Questions = Apply-VMHostProfile -Profile $HostProfile -Entity $VMHost -ApplyOnly -Confirm:$False
$QuestionsWithAnswers = @{}

We now need to have a source for the answers and pair them up with the questions, in my environment we have a large number of vmkernel ports on distributed switches and these are the only questions in the  host file that require answering.  These same techniques can be modified to work in other environments.  To provide the answers for the vmkernel ports (the IP address and subnet mask). I actually pull in an object created in another function which will create it dynamically for each host, this could be done manually or pulled in from a CSV.  The object looks something like:

[PSCustomObject]@{
PortGroup = "abd1-stor1-1234"
IP = "10.0.0.56"
Subnet = "255.255.255.0"
}

A collection of the Objects above would be stored in the $Answers variable, to match this up with the questions:

FOREACH ($Answer in $Answers)
	{
	$Question = $Questions.GetEnumerator() | Where {$_.Name -like "*$($Answer.PortGroup)`"*.address" -or $_.Name -like "*$($Answer.PortGroup)-management`"*.address" -or $_.Name -like "*$($Answer.PortGroup)-vmotion`"*.address"}
	$QuestionsWithAnswers += @{$Question.Name = $Answer.IPAddress}
	
	$Question = $Questions.GetEnumerator() | Where {$_.Name -like "*$($Answer.PortGroup)`"*.SubnetMask" -or $_.Name -like "*$($Answer.PortGroup)-management`"*.SubnetMask" -or $_.Name -like "*$($Answer.PortGroup)-vmotion`"*.SubnetMask"}
	$QuestionsWithAnswers += @{$Question.Name = $Answer.SubnetMask}
	}

The first line within the foreach loop here is filtering the Questions object for any question which matches up with the current answer in the loop.  The next line then adds that question answer pair to the hash table we initialized earlier. The first part answers all the IP address questions the second part is answering the same way for all the subnet masks.

Once we have a complete set of Questions paired up with answers we can now apply the host profile to the required host.

Apply-VMHostProfile -Entity $VMHost -Profile $HostProfile -Variable $QuestionsWithAnswers -Confirm:$false

This is simply applying the host profile again as we did in the first step except this time we are providing the answers to the questions at the same time.  This should now successfully apply to the host.  Any questions please post a comment below.

Comments