Automating ESXi Host Profile and Answer File with Powercli

Recently we have had an influx of hosts that need to be built out and I am trying to automate as much of the process as possible.  In this post will go over how the answer file works.  Next post I will talk about passing a CSV file to do multiple hosts.  Unfortunately, we do not use DHCP, PXE or Autodeploy so we have to have some manual input for both the install phase and the host profile phase.  So far, I have not found too much information out there on doing this so I have been doing some digging of my own.

Using the great Vmware Vsphere Powercli Reference book, by LucD, Alan Renouf I took their section on automating esxi deploys and tailored it to our needs.  The hardest part is that for our host profile there were answers that the host profile needed.   Namely, Management IP for the 1000v (DVS), VMotion address, and Subnet masks.

Using the Cmdlet Apply-VMHostProfile we are able to get the hash table keys that your host profile needs for input.

For example attach your host profile to your host: Apply-VMHostProfile -profile $profilename -entity $VMhost -AssociateOnly

You can then find out find out what keys are needed for your host profile.  In our case I have disabled the isci parameter and the MAC address parameter  so the only need information will be for our 1000v DVS which we have not added these hosts to yet.  To disable asking for a mac address you can follow this guide:

$AdditionalConfiguration = Apply-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

The out put will look like below, which as you can see with the key names so long you can’t get the whole key.

PowerCLI U:\powercli\hostprofile> $AdditionalConfiguration.GetEnumerator()| select Name


A better output would be to pipe the output to a list:

$AdditionalConfiguration.GetEnumerator()| fl

Once you have the key names, you can then create an answer file.  To create an answer file run the first command and apply it to a variable which is a hash table

$AdditionalConfiguration=Apply-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false

From here there are 2 options.  You can either assign the hashtable values manually or you can create a new hash table using wildcards for the key name and assign your variables. Once, you have your hash table created you can apply the host profile.  I did have an issue where if you don’t answer all the questions needed it will not apply the host profile it will just print out your keys and values plus the empty ones.

Assigning directly:

$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-management-management”].ipConfig.IpAddressPolicy.address’] = “”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-management-management”].ipConfig.IpAddressPolicy.subnetmask’] = “”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-vmotion-vmotion”].ipConfig.IpAddressPolicy.address’] = “”
$AdditionalConfiguration[‘network.dvsHostNic[“key-vim-profile-host-DvsHostVnicProfile-SVD-VSM03-vmotion-vmotion”].ipConfig.IpAddressPolicy.subnetmask’] = “”

Or create a switch and looking for a wildcard for your variables that need answers:

$var = @{}
switch ($AdditionalConfiguration.GetEnumerator())
{$ -like ‘*management*.address’ }    {
$var += @{$_.Name = “”}
{$ -like ‘*management*.subnetmask’}    {
$var += @{$_.Name = “”}
{$ -like ‘*vmotion*.address’}    {
$var += @{$_.Name = “”}
{$ -like ‘*vmotion*.subnetmask’}    {
$var += @{$_.Name = “”}
{$ -like ‘*vmotion*.mac’}    {
$var += @{$_.Name = “”}
{$ -like ‘*management*.mac’}    {
$var += @{$_.Name = “”}

Depending on whichever method you used you could run either of these commands to actually apply your profile. Your host must be in maintenance mode first and we can go over how to automate that in the next post.

Apply-VMHostProfile -Entity $hostname -Variable $var -Confirm:$false

Apply-VMHostProfile -Entity $hostname -Variable $AdditionalConfiguration -Confirm:$false

I have part 2 linked below that shows how to pass a csv file with the information and apply profiles to hosts:

Automating ESXi Host Profile and Answer File with Powercli (Part 2)


One Response to Automating ESXi Host Profile and Answer File with Powercli

  1. May says:

    Hi, I am working on similar requirement, using auto-deploy – however there are few caveats with auto-deploy which means there is a lot of processing to be done after initially booting up hosts. Would be great if you can share your email id so I can direct queries towards you as I go through it

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: