Automate ESXI Host Configuration with PowerCLI and CSV

By | March 5, 2017

Automate ESXI Host Configuration with PowerCLI and CSV

This post is about automating ESXI host configuration by using a CSV file. First, it is worth noting that if you’re a VMware customer that is licensed for Host Profiles, then I would suggest using Host Profiles to automate configuration. Configuring multiple ESXI hosts using host profiles is a well-documented method of configuration. However, if your VCenter licensing doesn’t cover Host Profiles, or you think configuring Host Profiles can be a little flaky sometimes( as I’ve seen it to be), then this is the post for you.  Also, this script configures Standard vSwitches, with the assumption that you’re a VMware customer that is not licensed for Distributed Switches.  So below are the list of tasks that this script will handle.

  • Configure HostName
  • Configure DNS Servers for Host
  • Configure Domain Information
  • Configure NTP Server
  • Configure ISCSI based on VMware Knowledge Base Article “Considerations for using software iSCSI port binding in ESX/ESXI
  • Configure ISCSI initiator ports
  • Configure ISCSI Targets
  • Configure VMotion
  • Configure Physical NICs ( Place NICs on desired Vswitches and configure teaming policies )
  • Configure VM Networking for up to 4 Networks

In our example, we will assume the host has 10 physical network adapters, with vmnic0 attached to the 1st vSwitch for management by default. Look for updates to this post where I upload scripts for hosts with 8, 4 and 2 physical network adapters. One prerequisite for the script to work is that a management IP address must be configured on the host prior to running the script. We will connect to the host by using PowerCLI and we will need an IP address on the host in order to connect. Also, make sure you have the latest version of PowerCLI installed before attempting to run the script. 

1. Download one of the following zip files based on the physical NIC configuration of your hosts. Create a C:\Temp directory on your workstation and extract the zip files to that location. The script specifically looks in that directory in order to run.

ESXI_Host_Configs_10NIC_ISCSI.ZIP

2. Open the Excel Document. You will see a number of column headers. If you hover over each of the headers, you will see a comment that explains what should be filled in.

I will break down what goes in each column header below…

  • HostIP – Management IP of ESXI host
  • Hostname – Hostname of ESXI host ( Ex. MYCOMPANYESXIHOST1 )
  • DNSIP – DNS server IP of 1st DNS server that ESXI host will use
  • DNSIP2 – DNS server IP of 2ND DNS server that ESXI host will use
  • DomainName – DNS domain of ESXI HOST ( Ex. Mydomain.com )
  • NTP – Time Server IP for ESXI host
  • MultiSubnetISCSI – Answer “Yes” or “No”, if your ISCSI network uses multiple subnets. This will determine how the ISCSI vSwitches are configured.
  • ISCSI1 – Enter the IP address of your first ISCSI initiator. These will be different per host.
  • ISCSI2 – Enter the IP address of your second ISCSI initiator. These will be different per host.
  • ISCSISUBMASK – Enter the subnet mask for the IP addresses of your ISCSI initiators.
  • ISCSITARGET1 – Enter the IP address of the first ISCSI SAN target. This will be the same per host.
  • ISCSITARGET2 – Enter the IP address of the second ISCSI SAN target. This will be the same per host.
  • ISCSIVLAN1 – Enter the VLAN for the IP address of your first ISCSI initiator. ( ISCSI1) If your ISCSI subnet is untagged, leave blank.
  • ISCSIVLAN2 – Enter the VLAN for the IP address of your second ISCSI initiator. ( ISCSI2) If your ISCSI subnet is untagged, leave blank.
  • VMotionIP – Enter the IP address of the VMotion vmkernel port.
  • VMotionMask – Enter the subnet mask for the VMotion vmkernel port IP address.
  • VMotionVLAN – Enter the VLAN of the VMotion vmkernel port IP address. If your VMotion vmkernel port is untagged, leave blank.
  • HostPassword – Enter the root password for you ESXI host.
  • PhyISCSINIC1-2 – These represent the physical network adapters that will be used for ISCSI. Because this example assumes a 10 NIC configuration and vmnic0 is already used for management, your options are vmnic1 through vmnic9. ( Note these cannot overlap with previously used vmnic adapters. )
  • PhyMANNIC2 – This represents the physical network adapter that will be used for the management network. Essentially this network adapter will be teamed with vmnic0 on the management vswitch. Your options are vmnic1 through vmnic9. ( Note these cannot overlap with previously used vmnic adapters. )
  • PhyLANNIC1-4 – These represent the physical network adapters that will be used for the virtual machine network vswitch. These 4 network adapters will be teamed in an active-active configuration for the virtual machine network. Your options are vmnic1 through vmnic9. ( Note these cannot overlap with previously used vmnic adapters. )
  • PhyVMONIC1-2 – These represent the physical network adapters that will be used for the vmotion vswitch. Your options are vmnic1 through vmnic9. ( Note these cannot overlap with previously used vmnic adapters. )
  • VMNET1NAME-VMNET4NAME – These represent the names of the virtual machine port groups that will be used for VMTraffic.
  • VMNET1VLAN-VMNET4VLAN – These represent the VLANs corresponding to each virtual machine port group.

Below are screenshots of a sample configuration. There’s also a sample configuration file within the zip file.

3. Now let’s take a look at the script itself. I won’t get too detailed into the script as each section is commented based on its function, but I will just give a general overview. We are using a foreach loop to process each line in the CSV file. Everything within the foreach loop is processed for each line in the CSV and then moves on to the next. For our purposes, each line represents an ESXI host. We configure one host and then move on to the next. We are saving a transcript of the entire session to the C:\temp\ESXI_Host_Configs_10NIC_ISCSI directory for the purpose of troubleshooting any errors within the session. You also may take notice of line 33 where we enable the ESXI Shell and SSH access on the host. Feel free to tweak this if needed. But remember, everything is setup to where you should not need to edit the script! Just update the csv file and run the PowerShell script!  As the script is processed you will see a progress bar for each host being configured, and the script will end with a “!!!Host Configurations Complete!!!” prompt.

4. Now we will run this script against two hosts. First, let’s take a loot at the default network config of one of our sample hosts. This is a typical out of box ESXI configuration, with the first vSwitch0 attached to vmnic0.

5. Now we will edit our excel document, (ESXI_HostConfigs_10NIC_ESCSI.xlsx), and save the file as a CSV file with the same name once complete. Don’t change the name of the file as the script is expecting the file to be named a certain way and in a certain location. ( Line 11 in the script. ) The screenshots below shows my sample excel file plus the file location that the file is saved to. You will get a prompt once you convert the file from .xlsx to .csv. Click yes to the prompt. The final screenshot shows the location of our files.

 

6. Now let’s open a PowerShell session. Run the following command to make sure we are reading the .csv file correctly. As we can see in the screenshot we are reading the file as expected.

7. From the PowerShell window, browse to the directory that the script resides in. “C:\temp\esxi_host_configs_10nic_iscsi\”. Then run the following command to run the script. The screenshots below shows various stages of the script configuration.

8. That completes our host configuration. Now let’s have a look at one of our hosts. We have fully configured Standard VSwitches. VLANs are configured for each virtual port group, network adapters are set up and teamed with the appropriate configurations. Best practices MTU sizes ( 9000 ) are configured for the ISCSI network.

9. Now let’s take a look at the configuration of the ISCSI Software Adapter. Notice the port bindings are configured and compliant. If we look int the Dynamic Discovery tab, we can see that our SAN Targets are configured.

10. Checking the DNS and Routing tab we can see that our DNS information has been configured. We can also check the Time Configuration tab, to see that NTP has been fully configured.

 

 

11. Finally, the entire session is logged in the file ESXI_Host_Config_Log.txt in the “C:\temp\ESXI_Host_Configs_10NIC_ISCSI” directory.

This completes this post on automating ESXI host configuration with PowerCLI. Remember that this post will be updated to reflect different server layouts with 8, 4 and 2 network adapters. We will also update posts that will include pure fiber channel options, essentially removing the ISCSI configuration. This method of automation should help to increase the deployment time of multiple ESXI hosts in environments not licensed for Host Profiles. This method can also allow consultants to send out pre-install docs to customers and also reduce deployment times. I hope you all found this post interesting and helpful. Until next time…

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *