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

My previous post  we did a single host profile by creating an answer file and then applying the host profile.  What if you have 10, 20, 30 hosts?

We will use the same functions, but read in a spreadsheet that contains our information and loop through each line in order to apply that to each host. Depending on what answers you need will dictate what is in your csv spreadsheet.  For our environment, we need Management IP, Subnet, Vmotion IP, and subnet.

So, the CSV would look like this:

Untitled

From here you will want to import your CSV and set to a variable:

#Import spreadsheet with hostname,mgmtip,mgmtsubnet,vmoip,vmosubnet for 1000v DVS input
$answerfile = Import-CSV ProfileAnswers.csv

You now have your answer file array set. We will loop through each item and repeat the code in the previous post to apply the host profile:

#Loop through each row and apply host profile

foreach($data in $answerfile) {

#Set VMhost variable to be able to pipe objects

$VMhost = Get-VMhost $data.hostname

Write-Host “Starting $VMhost.name”

#Attach host profile to host

Apply-VMHostProfile -profile $data.profile -entity $VMhost -AssociateOnly -confirm:$false | Out-Null

#Get hash table for answer file and assign to configuration

Write-Host “Getting Hash Table Answer File”

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

#Switch Statement to read in hash table and then set keys for Vmotion IP and Management IP on DVS

$var = @{}

If($AdditionalConfiguration){

switch ($AdditionalConfiguration.GetEnumerator())

{

{$_.name -like ‘*management*.address’ }   {

$var += @{$_.Name = $data.mgmtip}

}

{$_.name -like ‘*management*.subnetmask’} {

$var += @{$_.Name = $data.mgmtsubnet}

}

{$_.name -like ‘*vmotion*.address’} {

$var += @{$_.Name = $data.vmoip}

}

{$_.name -like ‘*vmotion*.subnetmask’}    {

$var += @{$_.Name = $data.vmosubnet}

}

Default {

$value = Read-Host “Please provide a value for ${$_.Name}”

$var +=@{$_.Name = $value}

}

}

}

#Set Host in maintenance mode, apply profile with answer file,  test for compliance

Write-Host “Apply Profile to $VMHost”

Set-VMHost -VMHost $VMhost -State ‘Maintenance’ | Apply-VMHostProfile -Variable $var -Confirm:$false | Test-VMHostProfileCompliance

}

Advertisements

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

  1. Kevin says:

    How would you do this to create virtual machine port groups and assign VLAN IDs to each port group? Also, is there a way to force prompting for user input for virtual machine port groups?

    • markdjones82 says:

      Kevin,
      I currently don’t have a lab up to test this, but normally if you have a host that already has the standard switch created, you can create the profile from that host and when you apply the profile it will automatically create all your VM network switching.

      Your other option is to use Powercli to create the switch and VLans and you can script it out that way.

  2. Pingback: Automating ESXi Host Profile and Answer File with Powercli | NutzandBolts - An IT Systems Engineer Experiences

  3. Steve says:

    I want to automate the changing if the hostname as well when this happens. Currently my hosts are in vCenter with the hame autogenerated when they picked up a DHCP address. The rest of your script is working great and the part that I added for the host name seems to be ignored despite the fact that I have added a column named hostName to the csv file??? I have edited the host profile setting for Hostname to ‘User specified host name to be used while applying the configuration.’ .Not sure what else I can or need to do to make this work?

    $csv = Read-Host “Enter the full path to the CSV file you are using, for example C:\temp\Profileanswers.csv”
    $answerfile = Import-CSV $csv

    #Loop through each row and apply host profile

    foreach($data in $answerfile) {
    #Set VMhost variable to be able to pipe objects
    $VMhost = Get-VMhost $data.hostname
    Write-Host “Starting $VMhost.name”
    #Attach host profile to host
    Apply-VMHostProfile -profile $data.profile -entity $VMhost -AssociateOnly -confirm:$false | Out-Null
    #Get hash table for answer file and assign to configuration
    Write-Host “Getting Hash Table Answer File”
    $AdditionalConfiguration = Apply-VMHostProfile -profile $data.profile -entity $VMHost -ApplyOnly -confirm:$false
    #Switch Statement to read in hash table and then set keys for Vmotion IP and Management IP on DVS
    $var = @{}
    If($AdditionalConfiguration){
    switch ($AdditionalConfiguration.GetEnumerator())
    {
    {$_.name -like ‘*hostName*.name’ } {
    $var += @{$_.Name = $data.hostName}
    }
    {$_.name -like ‘*management*.address’ } {
    $var += @{$_.Name = $data.mgmtip}
    }
    {$_.name -like ‘*management*.subnetmask’} {
    $var += @{$_.Name = $data.mgmtsubnet}
    }
    {$_.name -like ‘*vmotion*.address’} {
    $var += @{$_.Name = $data.vmoip}
    }
    {$_.name -like ‘*vmotion*.subnetmask’} {
    $var += @{$_.Name = $data.vmosubnet}
    }
    {$_.name -like ‘*NFS*.address’} {
    $var += @{$_.Name = $data.NFS}
    }
    {$_.name -like ‘*NFS*.subnetmask’} {
    $var += @{$_.Name = $data.NFSSubnet}
    }
    Default {
    $value = Read-Host “Please provide a value for ${$_.Name}”
    $var +=@{$_.Name = $value}
    }
    }
    }
    #Set Host in maintenance mode, apply profile with answer file, test for compliance
    Write-Host “Apply Profile to $VMHost”
    Set-VMHost -VMHost $VMhost -State ‘Maintenance’ | Apply-VMHostProfile -Variable $var -Confirm:$false | Test-VMHostProfileCompliance
    }

    • markdjones82 says:

      Steve, are you using Autodeploy or just booting with DHCP and then runnning this script to join them? I think if using DHCP it will not allow you to change that option because it picks it up from DHCP. You would need to put reservations in yoru DHCP for your hosts with the names that you want.

      • Gott, Steve (CORP) says:

        Mark,

        I had been using Autodeploy but having problems there so I am just installing the OS from the ISO image and then letting the host pick up a DHCP address.

        Once it has a DHCP Address I join it to the vCenter, manually configure the first host, create my profile and then run a script that should apply the host profile to the remaining hosts.

        The script picks up all of the data from the CSV file except the hostname even though I have a column in the CSV file for that purpose. The script prompts me for the hostname at run time and if I enter it manually there it applies it. I just don’t understand why it will not read that value in from the CSV file like it does the other data?

        I am building 96 hosts at a time so this really is quite time consuming to babysit the script for each one to be added manually but it is still better then manually keying in the data to the answer file.

        Steve

  4. markdjones82 says:

    Steve, are you getting any errors? Can you run this command on 1 host that needs to be remediated and tell me the output? I suspect still that with DHCP enabled it will not allow you to change that value.

    $AdditionalConfiguration = Apply-VMHostProfile -profile $profilname -entity $VMHost -ApplyOnly -confirm:$false
    $AdditionalConfiguration.GetEnumerator()| select Name

    Also, you might want to post this on the Powercli Community forums and we can start a thread over there where others can chime in as well.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: