First things first, let's begin with pseudocode/documentation of what you want to accomplish. In between each of these sections, you will insert the code you have previously developed individually and put them into a full file:
Since you want this script to do as much without any manual intervention, you will want to try and eliminate as many prompts as possible. Since you will be connecting to and executing commands on multiple ESXi hosts, you would normally get prompted to login each time you connect to a host. To avoid this, you can store the credentials in a variable and pass them to each connect-viserver
cmdlet.
Note
When you first covered connecting to ESXi servers from PowerCLI, you experienced the login box for the host. The Get-Credentials
cmdlet causes the same action but returns a credentials object that can be reused. For now, you'll proceed with the stored credentials and you will use them in a later step.
You're going to use an array of hostnames to connect to individual ESXi hosts for configuration. To create the array, you set a variable and store a comma separated list of addresses to connect to. The addresses can either be hostnames or IP addresses. For this example, you will use IP addresses, but they can easily be fully qualified domain names:
For the network configuration settings, you will need to set up some additional settings. Since each host has three additional vmkernel ports configured, you need to build a different address for each of these to be used in Step 8. To allow this, you will create three additional variables that contain the first three octets of the network for each vmkernel port:
The next step is to go back and pull in all of the code you had previously written in one form or another. For this, you will reuse the ForEach
loop to execute the cmdlets on multiple ESXi hosts:
The curly brace marks the beginning of the ForEach
loop. You will close the loop with a right curly brace later in the script. Inside the loop, you're going to include Steps 4 – 9 from the outline.
For the next step, you're going to use our stored credentials to connect to an ESXi host. Immediately after this, you will store our VMHost object for use throughout the rest of the loop:
For the next several steps, you're just going to pull code you have already developed. Since each step was covered in depth, you will just bring over the code:
For the network settings, you will need three additional IP addresses for the vMotion, Storage, and FT Logging vmkernel ports. You will compute these addresses using the last octet of the service console IP. To do this, you will first retrieve the IP address of the host:
Next, you will split the string based on the period between octets, then take the last octet of the IP address and store it as a variable. The IP is in a property called IP
. To split that IP into an array, you will use the Split()
method, which is a built-in PowerShell method that transforms a string into an array by separating characters with the character passed into the method.
For instance, you want to separate the string at the periods of the IP address, so you pass "."
into the Split()
method. Since the Split()
method turns it into an array, you can then reference the element you want to return – the fourth element. However, remember arrays begin count at 0, so you will return element 3 using square brackets.
Note
Because data is stored in objects, objects have both properties and methods. Methods perform operations on the data of the object and properties contain the data of the object. In subsequent recipes throughout this book, you will look at and use other methods to gain more experience using built-in PowerShell functionality to manipulate data stored in objects.
The last step to build the address for this host in the ForEach
loop is to concatenate the final octet with the network strings to build a full IP address:
Now that your unique IP addresses are created on the three additional networks, you can use them with the cmdlets you wrote in the Setting network configuration recipe.
The final part of the ESXi host configuration is closing the ForEach
loop and then disconnecting from this host so that you can connect to the next host:
At this point in the initial configuration, you would want to format your datastores on iSCSI or Fibre Channel arrays, but this is not really a repeatable set of steps. I would suggest one of the two things—either configure the datastore manually from PowerCLI or configure it from the GUI and then come back and run the remainder of the script. Since the focus of this example is to make a repeatable configuration script, the datastore formatting doesn't fit since it is a command used just one time .
The next step is to take our hosts and connect them to vCenter. The easiest way to do this is to connect to vCenter and then use Add-VMHost
to add them into inventory. While in the same ForEach
loop to accomplish this, you can set central syslog and rescan the hosts for all storage changes:
Note
For the purpose of this script, you are going to assume that vCenter already has a datacenter created and named "Primary." You will use this location to place the ESXi host into vCenter.
Next, you will run through an additional ForEach
loop to add the hosts and set their settings in vCenter:
Now, you are ready to add the host into vCenter from the Joining an ESXi host to vCenter recipe:
After adding the host to vCenter, you want to store a VMHost
object pointing to the host to use with later cmdlets in this loop:
For the next few steps, you will pull the host settings related to rescanning for datastores and setting the syslog settings:
Finally, you will close the loop with a right curly brace.