Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
PowerCLI Cookbook
PowerCLI Cookbook

PowerCLI Cookbook: Over 75 step-by-step recipes to put PowerCLI into action for efficient administration of your virtual environment

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

PowerCLI Cookbook

Chapter 1. Configuring the Basic Settings of an ESXi Host with PowerCLI

In this chapter, you will cover the following recipes:

  • Connecting to an ESXi host or a vCenter instance
  • Getting the VMware host object
  • Joining an ESXi host into Active Directory
  • Enabling services and setting security profiles
  • Setting network configuration
  • Creating datastores on an ESXi host
  • Configuring syslog settings on a host
  • Joining an ESXi host to vCenter
  • Creating a configuration script to set all properties uniformly

Introduction

Initially, automation doesn't save time. To get the benefits of automation, you must invest the time upfront to create scripts that you'll use time and again. In this chapter, you'll take your first ESXi host that has been installed with an IP address configured on it, and you will configure the server continually from there. This chapter will take an administrator through the basic configuration tasks needed to perform the initial configuration, join them to vCenter, and get it into an operational state. At the end of this chapter, all of these steps will build into a scripted configuration that can be executed against new hosts in the future.

Connecting to an ESXi host or a vCenter instance

To begin working with PowerCLI, you must first have PowerShell installed and available on the system on which you will run PowerCLI. PowerShell is a part of the Windows Management Framework and it ships with Windows client and server versions. PowerCLI extends PowerShell with commands to administer VMware environments. With PowerShell installed, you will need to obtain PowerCLI from http://www.vmware.com. The specific link is listed in the See also section of this recipe.

Once you have installed PowerCLI, you will need to build an ESXi host for this recipe. All that is required is a fresh ESXi installation from the ISO or DVD image distributed by VMware. Once installed, set an IP address on an accessible network using the console screens of the new ESXi host. The network address should be accessible from your PowerCLI workstation.

With the assumption that your ESXi host is built, the first step to administer VMware environments in PowerCLI is to connect to the ESXi host or to a vCenter server. In this chapter, you will focus on how to configure a single ESXi host. In the next chapter, you will focus on how to configure a vCenter Server and a vSphere cluster of ESXi hosts.

Getting ready

To begin, you only need to launch PowerCLI from its shortcut on the desktop or from the Start Menu. If you already had PowerCLI previously installed, you will want to check the version number to ensure that the cmdlet references throughout the book are available to you. Each version of PowerCLI builds additional native cmdlets and functionalities. To check the version you are running, open a PowerCLI prompt and run Get-PowerCLIVersion.

The recipes in this book are built and tested using VMware PowerCLI 5.5 Release 1 and have also been tested with VMware PowerCLI 5.8 Release 1, which accompanies vCloud Suite 5.8. The recipes also work on vSphere and PowerCLI 6.0 with any differences noted.

How to do it...

  1. At the PowerCLI prompt, you will execute the Connect-VIServer cmdlet as follows:
    Connect-viserver <hostname or IP>
    
  2. When executed, the code will attempt to perform a single sign-on into the ESXi host, but unless your username is root and you set the same password locally and on ESX, single sign-in will fail. You will be prompted with a normal Windows login window, which is displayed as follows, and you should log in with the root username and the password you specified during your ESXi installation:
    How to do it...
  3. Once you successfully log in to the ESXi host, a confirmation message will be displayed with the name or IP address of the ESXi host you connected to, the port, and the user you've connected to, as shown in the following example:
    Name                           Port  User
    ----                           ----  ----
    192.168.0.241                  443   root
    
  4. At this point, the PowerCLI session is connected to a host and ready to execute work.

How it works…

The Connect-VIServer cmdlet is the simplest kind of cmdlet in PowerCLI. This cmdlet initiates a connection to the vCenter or ESXi web services to allow additional commands to be passed to the server and be executed.

The Connect-VIServer cmdlet requires only the name of the host to which you want to connect. There are additional parameters that you might pass to the cmdlet, such as the protocol (HTTP or HTTPS), the username, and the password. If you prefer not to keep your password in plain text, you can also pass a PSCredentials object. The PSCredentials object contains login data to authenticate. For more information about the PSCredentials objects, type get-help about_server_authentication.

Once you execute the cmdlet, a warning will be displayed in yellow, similar to the following one:

How it works…

The warning is displayed because the certificate installed on the ESXi host is self-signed and untrusted by the computer you are connecting from. Changing an SSL certificate on ESXi hosts will be covered later in the book, but the warning can be ignored this time. The cmdlet will continue to execute even though the warning is displayed.

You can also prevent the invalid certificate errors by running the following PowerCLI cmdlet that changes the action when an invalid certificate is encountered:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false

There's more…

If you choose to join the ESXi host to Active Directory, your PowerCLI session performs a single sign-in. PowerCLI uses the credentials of your current Windows session to log in against the ESXi host or vCenter server if your account has access to the server. If your account does not have access to the server that it is attempting to connect to, a login box will be presented like our example, shown in the preceding screenshot, in this recipe.

See also

Getting the VMware host object

Cmdlets become available to manage a host after we connect to that host to manage it. The first concept that you will need to become aware of are PowerShell objects. Objects are defined as data obtained from commands that run in PowerShell and PowerCLI. To perform configuration on an ESXi host, the commands that you run will need a host object, which is specified.

In this recipe, you will learn how to obtain a VMHost object.

Getting ready

To begin with, open a PowerCLI window and connect to an ESXi host or a vCenter instance.

How to do it...

  1. PowerCLI is straightforward. To retrieve an ESXi host object, just run the following command line:
    Get-VMHost
    
  2. After running the Get-VMHost cmdlet, an object that contains one or more ESXi hosts is returned. You are connecting to a single ESXi host in this example and running Get-VMHost that returns the host object with a single host. If you were connecting against a vCenter instance, Get-VMHost (with no other arguments) would return an object that contains all of the hosts managed by vCenter. When running against vCenter, you can specify a filter with the Get-VMHost cmdlet in order to find one or more hosts that match the specified pattern:
    Get-VMHost esxhost*
    Get-VMHost VMHOST1
    
  3. Instead of calling the Get-VMHost cmdlet each time, you need to get the ESXi host. You can store the host object in a variable. PowerShell variables are specified using $ followed by a name. The following is an example of our ESXi host:
    $esxihost = Get-VMHost
    

How it works…

To learn more about the VMHost object, you can use the Get-Member cmdlet with the variable you have just defined. To use Get-Member, you will call the VMHost object by typing the $esxihost variable. Then, you pipe the object into the Get-Member cmdlet as follows:

$esxihost | Get-Member

PowerCLI is an extension of PowerShell that is used specifically for VMware product management. PowerShell is an object-based language that uses the concept of encapsulating both data and operations within an object data type, which is a familiar object-oriented programming concept. Objects have defined data areas and can include functions that perform operations on the data in the object.

The output from the cmdlet shows all of the data contained in the Property elements in the object. The object also includes a number of methods. These methods are used to manipulate the data in the object. The output of the preceding command is shown in the following screenshot:

How it works…

You can call a method by using a dot notation (.) and by calling the method name followed by parenthesis, such as in the following example:

$esxihost.ConnectionState.ToString()

In the preceding example, the State property is an object inside the VMHost object, but the ToString() method converts the output to a string.

Now that the ESXi host object is stored in a variable, you can proceed with other cmdlets for configuration and run them using the host object to perform the configuration.

There's more…

Get-VMHost has other applications other than just returning the VMHost object to use. Like all other Get- cmdlets, this cmdlet can be used to find a host in a particular configuration or state. You can use Get-VMHost to find hosts assigned to a particular location in vCenter using the -Location parameter. You might want to find hosts that have been assigned a particular tag in vSphere using the –Tag parameter or you might want to find the host running a particular VM with the -VM parameter. Another interesting use case is specifying the -Datastore parameter to find all of the hosts that have a particular datastore connected.

Get-VMHost is just one of the many cmdlets that work with VMHost objects. Others will be explored in Chapter 2, Configuring vCenter and Computing Clusters.

See also

  • The Setting up folders to organize objects in vCenter recipe in Chapter 2, Configuring vCenter and Computing Clusters
  • The Creating basic reports of VM properties using VMware Tools and PowerCLI recipe in Chapter 3, Managing Virtual Machines

Joining an ESXi host into Active Directory

As mentioned in the connecting section, joining an ESXi host to Active Directory offers the ability to connect to the host without entering the credentials for administrators. Active Directory is a Windows implementation of Lightweight Directory Access Protocol (LDAP). It contains accounts for users, computers, and groups. It runs on a Windows Server that has the Active Directory role installed and that has been "promoted" to become a domain controller. To perform this recipe, you will need at least one Active Directory server available on the network with the ESXi host.

Seamless authentication is one of the biggest reasons to join a host to Active Directory. However, beyond single sign-on, once the ESXi host is connected to Active Directory, groups in the directory can be leveraged to grant permissions to the ESXi host. If you do not have Active Directory installed and do not wish to, you can skip this recipe and move on to other topics of host configuration without any impact to future recipes.

Getting ready

PowerCLI has Get-VMHostAuthentication and Set-VMHostAuthentication, two cmdlets to deal with host authentication. To get ready to set up authentication, open a PowerCLI window and connect to a single ESXi host.

How to do it...

  1. Because the cmdlets require a VMHost object, you'll again be using Get-VMHost to either populate a variable or to pipe an object to the next object. The first step is to obtain a VMHost object for our target ESXi host. This can be done using the following command line:
    $esxihost = Get-VMHost 192.168.0.241
    
  2. Once you have your VMHost object, you can look at setting up the authentication. The Set-VMHostAuthentication cmdlet needs to be executed. The cmdlet requires several parameters to join an ESXi host to the domain. The syntax needed is displayed as follows:
    $esxihost | Get-VMHostAuthentication | Set-VMHostAuthentication -JoinDomain -Domain domain.local -user username -password *****
    
  3. Executing the cmdlet will prompt you to confirm that you want to join this host to the domain specified. If your answer is Y, the cmdlet will continue and execute the operation as follows:
    Perform operation?
    Joining VMHost '192.168.0.241' to Windows Domain 'domain.local'.
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
    (default is "Y"):Y
    
    Domain          DomainMembershipStatus    TrustedDomains
    ------          ----------------------    --------------
    DOMAIN.LOCAL    Ok
    

How it works…

One of the first things you will notice about this recipe is that there is an extra Get-VMHostAuthentication cmdlet in the middle of the command line. Why does it need to perform Get before performing Set? It would seem that you can simply pipe the VMHost object into cmdlet to specify your target host and the cmdlet will execute the function. But as you try that, using the following command line, PowerCLI displays an error, as shown in the following screenshot:

$esxihost | Set-VMHostAuthentication -JoinDomain -Domain domain.local -user username -password *****
How it works…

In this case, the cmdlet looks for a VMHostAuthentication object and not a VMHost object, so an error is displayed. If you go back and simply execute the Set-VMHostAuthentication cmdlet as follows, it will prompt you for a VMHostAuthentication object and wait for an input:

Set-VMHostAuthentication -JoinDomain -Domain domain.local -user username -password *****
How it works…

This is where the Get-VMHostAuthentication cmdlet gets added. It retrieves the VMHostAuthentication object from the host you targeted since this cmdlet accepts the VMHost object as a piped input.

The Get-Help cmdlet for Set-VMHostAuthentication also shows that the cmdlet expects a VMHostAuthentication object to be passed as a parameter for the cmdlet. By executing the cmdlet with all of its parameters and no piped input, you also learned that you can debug and learn what input the cmdlet is expecting and missing.

There's more…

The same cmdlets can also be used to remove a host from a domain, if needed. The -LeaveDomain parameter is a part of the Set-VMHostAuthentication cmdlet and allows this need.

In addition to setting up an ESXi host to accept Active Directory authentication, PowerCLI also provides a number of cmdlets to add local users, groups, and permissions inside a single ESXi host. The New-VMHostAccount cmdlet is used to create new users on an ESXi system. The same cmdlet previously allowed the creation of groups, but this functionality was removed with ESXi 5.1. There is a Set-VMHostAccount cmdlet to change accounts and group memberships, and a Remove-VMHostAccount cmdlet to remove a user or a group.

See also

  • The Setting permissions on vCenter objects recipe in Chapter 2, Configuring vCenter and Computing Clusters

Enabling services and setting security profiles

ESXi hosts enable a few services by default, but there are some additional services that are installed but blocked. In some cases, you might want to enable SSH on the host. However, since VMware does not recommend enabling SSH and will display a warning. You can set an advanced setting to disable this warning.

Getting ready

To begin with, you should open a PowerCLI prompt and connect to an ESXi or vCenter host. You will also want to store a VMHost object in a variable named $esxihost.

How to do it…

  1. The first step is to get the list of available services from a VMware host. To do this, you use the Get-VMHostService cmdlet and pass the VMHost object into the cmdlet as follows:
    $esxihost | Get-VMHostService
    
    How to do it…
  2. The output of the preceding cmdlet will display a list of the available services on the ESXi host along with its policy (whether it is set on or off by default) if it's running. The label is a friendly identifier to find the service you want to configure, but the key is the piece of data you will use to return the single service you want.
  3. In this case, we're looking to configure the service with the TSM-SSH key. To scope the results down to that one service in the object, you will use a PowerShell where clause as follows:
    $esxihost | Get-VMHostService | where { $_.key -eq "TSM-SSH" }
    
    How to do it…
  4. Now that you have it scoped down to a single service, you pass this object into the Set-VMHostService cmdlet with the desired policy of On as follows:
    $esxihost | Get-VMHostService | where { $_.key -eq "TSM-SSH" } | Set-VMHostService -Policy "On"
    
    How to do it…
  5. At this point, you have configured the host to autostart the service on boot, but the service is still not running in the current boot. To do this, you will instead use the Start-VMHostService cmdlet. Again, you have to pass in the VMHostService object for SSH (or any other service that you choose).
    $esxihost | Get-VMHostService | where { $_.key -eq "TSM-SSH" } | Start-VMHostService 
    
    How to do it…
  6. With the service running, vSphere displays the warning that you have enabled SSH. This will leave your host showing in a warning state as long as the service is running; however, VMware does allow you to suppress this warning, but this is set through an advanced setting. To set this, you need to execute the following cmdlet:
    $esxihost | Get-AdvancedSetting –Name UserVars.SuppressShellWarning | Set-AdvancedSetting –value 1
    
  7. When executed, the preceding command line will prompt you to confirm the settings. This confirmation can be suppressed using the –Confirm:$false common parameter, which is useful in scripts:
    $esxihost | Get-AdvancedSetting –Name UserVars.SuppressShellWarning | Set-AdvancedSetting –value 1 –Confirm:$false
    

How it works…

For configuring host services, the native cmdlets follow the expected pattern of Get and Set functionality in PowerCLI. Get-VMHostService expects a VMHost object as the input which is logical since these host services exist within the scope of a host. Once you get the host service by name and store it in a variable or pass it as an object in the pipeline, you can easily set the settings to the desired configuration. In addition to Get and Set cmdlets, you also have Start and Stop cmdlets. The Start and Stop cmdlets are more specific to this use case since we're dealing with host services and there is a specific need to start or stop them in addition to configuring them. The Start and Stop cmdlets also accept the HostService objects as inputs, just like the Set-VMHostService cmdlet.

In the specific use case of the SSH Server service, it causes a warning to be displayed to the client. To disable this warning from been displayed, you can use an advanced setting named UserVars.SupressShellWarning. While this is not recommended for production systems, there are plenty of use cases where SSH is needed and is helpful in lab environments, where you might want to configure the setting.

There's more…

The cmdlet to start the SSH service can be easily adapted beyond the illustrated use case with the use of a ForEach loop. For troubleshooting and configuration, you might need to enable SSH in order to tail a log file or to install a custom module. In these cases, starting SSH in bulk might be handy. To do this, you take the preceding code and wrap it in the loop. An example of a connection to a vCenter host, a variable with multiple VMHost objects returned, and a loop to step through and start SSH on each is shown as follows:

Connect-VIServer vcenterhost.domain.local
$esxihosts = Get-VMHost
foreach ($esxihost in $esxihosts) {
$esxihost | Get-VMHostService | where { $_.key -eq 
"TSM-SSH" } | Start-VMHostService 
}

This quickly allows you to turn on SSH for temporary use. Following a reboot, the service will no longer be running and you can easily change the preceding code to be a Stop-VMHostService cmdlet and turn off the service in bulk.

Setting network configuration

One of the first things to be completed against a new ESXi installation is network configuration. Network configuration consists of several things on an ESXi host—first would be to configure the additional management interfaces of the host for VMotion, Fault Tolerance logging, vSphere Replication, and VSAN traffic.

Getting ready

To begin this recipe, you will need to open a PowerCLI window, connect to an ESXi host, and load a VMHost object into a variable. The example uses $esxihost as the variable for the VMHost object.

On installation, ESXi has a single Network Interface Card (NIC) labeled eth0 that is connected to a VMware Standard—vSwitch. The vSwitch has two port groups created: one labeled Management Network for management traffic and the other is labeled VM Network. The Management Network is a vmkernel port with the IP defined on the console attached to it.

In this example, our host contains six 10 Gigabit NICs that will connect the host to the network. You will define two additional vSwitches with two physical ports attached to each for redundancy. The additional vSwitches will handle storage and replication traffic on one and VM traffic on the other.

Getting ready

Note

Best practices of vSphere networking are far beyond the scope of this book. The network layout shown in the preceding diagram is not an endorsement of a particular layout and is for illustration purposes to show the PowerCLI cmdlets used to configure networking on ESXi.

How to do it...

  1. To begin with, let's get an idea of the network layout that is in place, by default. From a default install, there is a single virtual switch named vSwitch0. The first cmdlet shows you the properties of this virtual switch and the second shows you the port groups associated with that vSwitch. To do this, review the output of the two PowerCLI cmdlets:
    $esxihost | Get-VirtualSwitch
    $esxihost | Get-VirtualPortGroup –VirtualSwitch vSwitch0
    
  2. The first thing to be completed is to remove the default VM Network port group, since it's not the best practice to have Virtual Machine traffic on the management ports, and this default port group is not a part of the design you outlined for this configuration:
    $esxihost | Get-VirtualPortGroup -Name "VM Network" | Remove-VirtualPortGroup –Confirm:$false
    
  3. The preceding command combines the Get-VirtualPortGroup and Remove-VirtualPortGroup cmdlets to change the confirmation. When executed, you will receive either a confirmation or an error. If the port group is connected to or in use by a VM, you will receive an error message. Once you remove the VM Network port group, the next step is to add an additional vmkernel port that will be used for vMotion.

    Note

    While this is outside the scope of this book, there are many different ideas for the best design of VMware networking. Most administrators agree that Management traffic and vMotion traffic should be separated, but with increasing speeds and capabilities of NICs today, it's common to see them sharing the same virtual switch. Administrators will set the Management traffic to be active on the first NIC and vMotion to be active on the second NIC. The two traffic streams will only be on the same NIC in a failover situation.

  4. In our design, you will set Management and vMotion to be collocated on the same switch. To do this, use the New-VMHostNetworkAdapter cmdlet and pass in the name of the port group, the virtual switch, and the IP information. You will also pass in a parameter to specify that this vmkernel port should be used for VMotion as follows:
    $esxihost | New-VMHostNetworkAdapter -PortGroup "VMotion Network" -VirtualSwitch vSwitch0 -IP 192.168.50.241 -SubnetMask 255.255.255.0 -VMotionEnabled $true
    
  5. In our design, although vMotion and Management traffic exist on the same vSwitch, the traffic will be separated using active and standby links on each port group. This is done by changing the NIC Teaming Policy with the Set-NicTeamingPolicy cmdlet. You can see in the following two commands that the active and standby NIC assignments are opposite between the two port groups:
    $esxihost | Get-VirtualPortGroup -Name "Management Network" | Get-NicTeamingPolicy | Set-NicTeamingPolicy –MakeNicActive vmnic0 –MakeNicStandby vmnic1
    
    $esxihost | Get-VirtualPortGroup -Name "VMotion Network" | Get-NicTeamingPolicy | Set-NicTeamingPolicy –MakeNicActive vmnic1 –MakeNicStandby vmnic0
    
  6. The port group is automatically created and the vmkernel/host port is created for our vMotion network, but it's on the wrong VLAN. Our vMotion traffic is on a different VLAN, so you need to set this on the port group as follows:
    $esxihost | Get-VirtualPortGroup -Name "VMotion Network" | Set-VirtualPortGroup –VlanID 50
    
  7. The next step is to create a new virtual switch with its own uplinks on vmnic2 and vmnic3, as shown in our diagram. To confirm that the physical NICs exist, you can run the following cmdlet:
    $esxihost | Get-VMHostNetworkAdapter
    

    The Get-VMHostNetworkAdapter cmdlet displays all of the vmkernel ports along with all of the physical NICs present on the host.

  8. After confirming the NIC, you will run the New-VirtualSwitch cmdlet to provision the new virtual switch. This cmdlet provisions the vSwitch with its uplinks, but it's currently an island with no connectivity for Management or virtual servers:
    $esxihost | New-VirtualSwitch -Name vSwitch1 -Nic vmnic2,vmnic3
    
  9. The next step is to create vmkernel ports for storage traffic and replication traffic. These are created in the same way as the VMotion Network we provisioned earlier:
    $esxihost | New-VMHostNetworkAdapter -PortGroup "Storage Network" -VirtualSwitch vSwitch1 -IP 192.168.100.241 -SubnetMask 255.255.255.0 -VsanTrafficEnabled $true
    
    $esxihost | Get-VirtualPortGroup -Name "Storage Network" | Set-VirtualPortGroup –VlanID 100
    
    $esxihost | New-VMHostNetworkAdapter -PortGroup "FT Logging Network" -VirtualSwitch vSwitch1 -IP 192.168.200.241 -SubnetMask 255.255.255.0 -FaultToleranceLoggingEnabled $true
    
    $esxihost | Get-VirtualPortGroup -Name "FT Logging Network" | Set-VirtualPortGroup –VlanID 200
    
  10. Again, you want to make sure that our Storage Traffic and Fault Tolerance traffic don't end up competing for bandwidth. Therefore, you will assign one port group to be active on one uplink and the other port group to be active on the second uplink. This is done again with the Set-NicTeamingPolicy cmdlet:
    $esxihost | Get-VirtualPortGroup -Name "Storage Network" | Get-NicTeamingPolicy | Set-NicTeamingPolicy –MakeNicActive vmnic2 –MakeNicStandby vmnic3
    
    $esxihost | Get-VirtualPortGroup -Name "FT Logging Network" | Get-NicTeamingPolicy | Set-NicTeamingPolicy –MakeNicActive vmnic3 –MakeNicStandby vmnic2
    
  11. The final step of our network provisioning is to create new port groups for Virtual Machine traffic. You have set all of the virtual machine traffic to its own vSwitch and uplinks in the design outlined. The first step is to create the virtual switch like you did for vSwitch1 as follows:
    $esxihost | New-VirtualSwitch -Name vSwitch2 -Nic vmnic4,vmnic5
    
  12. Once the virtual switch is created, you can create two port groups on the virtual switch. However, in this case, New-VirtualPortGroup doesn't allow any pipeline input, so you will need to specify the server as a parameter instead of passing it through the pipeline:
    New-VirtualPortGroup -Name "Infrastructure Network" -VirtualSwitch vSwitch2 -VLanId 1 -Server 192.168.0.241
    
    New-VirtualPortGroup -Name "Application Network" -VirtualSwitch vSwitch2 -VLanId 2 -Server 192.168.0.241
    

How it works…

In this example, you will work with the VMHost object to enumerate and identify the existing configuration that is put in place during the installation. From there, you remove the default VM networking configuration, you provision new virtual switches and vmkernel ports to segment traffic, and you enable certain management functions across the vmkernel ports.

While most of the configuration covered in this section deals with the initial configuration of a host, some of the concepts are repeated more often. For instance, if you have a multi-node cluster and you're adding a new virtual machine network, you'll use the New-VirtualPortGroup cmdlet often. As you have seen in previous examples, you can easily create an array of ESXi hosts—either by using Get-VMHost in vCenter or by manually specifying a list of hosts—and then connect and provision the same port group on many hosts, quickly. This would mean big time savings and less potential for manual error when compared to manually clicking on each through the GUIs to configure the new port group on each host in the cluster.

By also using the Set-NicTeamingPolicy cmdlet, you can set a preferred uplink port for each port group and put the other NIC into standby mode. This allows us to keep the Management and vMotion and the Storage and Fault Tolerance traffic separated so they will not cause the performance of one another to be degraded.

There's more…

In this recipe, you focused on VMware Standard vSwitches. Users with Enterprise Plus licensing also have the option of using VMware Distributed vSwitches which have their own set of cmdlets to manage and configure these advanced virtual switches.

See also

Creating datastores on an ESXi host

With networking, VMware has done a lot of work to ease administration with the VMware Distributed Virtual Switch. In vSphere 5.5, VMware introduced Datastore Clusters that alleviate some of the management of datastores. However, from a provisioning standpoint, the initial setup of storage is still manual and can take a lot of manual steps. Scripting this makes a lot of sense in large environments.

Datastore and storage under vSphere is also different since some operations must be performed on the raw storage device and these steps are not repeated on every host. There are three types of storage connectivity that you might need to provision: NFS, iSCSI, and Fibre Channel. For this example, you will focus on iSCSI and NFS, and you will work on provisioning storage from both. Along the way, Fibre Channel will also be discussed since its concepts overlap with iSCSI from a vSphere perspective.

Getting ready

For this example, you will need to open a PowerCLI window and connect to an ESXi host. You will also want to make sure that you have the VMHost object stored in a variable named $esxihost, covered in the Getting the VMware host object section.

How to do it…

  1. The simplest of all datastores to provision is an NFS datastore. A single PowerCLI cmdlet will provision an NFS datastore. The New-Datastore cmdlet will take all of the input needed to provision the new datastore and make it available for use. Since NFS does not use the VMFS filesystem, there are no filesystem properties that need to be passed. To connect NFS, you just need to provide a name for vSphere to identify the datastore, a path (the export), and the host that is providing the NFS, as follows:
    $esxihost | New-Datastore -Nfs -Name DataStoreName -Path /data1/export -NfsHost nfsserver.domain.local
    
  2. With this, you've got your first datastore presented and ready to host virtual machines. For NFS, this is all that is required.
  3. iSCSI and Fibre Channel storage is a bit more complex to provision from a PowerCLI and vSphere perspective. Provisioning storage on either of these protocols will require additional decisions to be made when creating the datastore. iSCSI also requires additional configuration steps that are not needed with Fibre Channel. We will focus on iSCSI in this example and I will make a note of where the concepts overlap with Fibre Channel.
  4. iSCSI is an IP-based storage protocol, and as such, you will need to do a bit of network configuration to set up iSCSI to work in our environment. The first thing that needs to be done is to enable iSCSI and to create a software iSCSI target, as follows:
    $esxihost | Get-VMHostStorage
    
  5. By default, there isn't a software iSCSI target that is created. To create this, you need to expand on the previous cmdlet and set this value to true, as follows:
    $esxihost | Get-VMHostStorage | Set-VMHostStorage -SoftwareIScsiEnabled $true
    
  6. The next step is to set the iSCSI targets using the New-IscsiHbaTargets cmdlet. This cmdlet requires that you pass in the iSCSI HBA as an object, so first, you retrieve the iSCSI HBA using Get-VMHostHba and store it in a variable and then use it with New-IscsiHbaTargets:
    $iSCSIhba = $esxihost | Get-VMHostHba -Type iScsi
    
    New-IScsiHbaTarget -IScsiHba $iSCSIhba -Address $target -ChapType Required -ChapName vSphere -ChapPassword Password1
    

    Note

    In the example, there are additional parameters for authentication. iSCSI uses Challenge-Handshake Authentication Protocol (CHAP) to authenticate sessions to the target storage. Authentication is not required and if the storage system is not configured for authentication, these parameters can be omitted. However, it's a bad practice to deploy a production storage array without authentication.

  7. The final step of the initial iSCSI configuration is to bind the iSCSI HBA to a specific port. Since you created a Storage Network management port, this is the port that you want to use. To make this change and to remove any other ports, you have to use the ESXCLI interface within PowerCLI. There isn't a native PowerCLI cmdlet for this function:
    $esxcli = Get-ESXCLI -VMHost $esxihost
    $esxcli.iscsi.networkportal.add($iscsihba, $true,"vmk2")
    
  8. In our case, the vmkernel port assigned to the Storage Network port group is vmk2. Using the ESXCLI interface, you can assign it to the iSCSI HBA. To confirm the change, you can use the list() method, as follows:
    $esxcli.iscsi.networkportal.list()
    
  9. As you will see, there are other vmkernel ports listed. In my case, vmk0; you can remove them with a simple remove() method, as follows:
    $esxcli.iscsi.networkportal.remove($iscsihba,$true,"vmk0")
    

    Now that the system has its targets configured, if the iSCSI array has provisioned storage to the host, it should be visible. This is the point where iSCSI and Fibre Channel converge. Since iSCSI uses the host bus adapter model that Fibre Channel invented, they work in the same way after initial configuration. You must run the NFS mount on each server and you must set up iSCSI initial configuration on each host. Scanning and formatting VMFS datastores only needs to be done from a single host for iSCSI and Fibre Channel disks since they are shared resources. This means that when scripting the steps on each host, the next few steps only need to be done on a single host in the cluster and then every host needs to be a rescan:

    $esxihost | Get-VMHostStorage -RescanAllHBA -RescanVmfs
    

    Starting with a rescan is a good idea so that your system recognizes all of the storage changes and sees all disks that have been presented. Whether you're using software or hardware iSCSI, Fibre Channel, or converged network adapters, this is the point where your hosts see its SAN disks.

  10. At this point, your ESXi system doesn't have iSCSI or Fibre Channel datastores that it can use. Even though the disk is visible, it is unformatted and not ready to host VMs. To discover your disks and to enumerate the data you need to configure it, you will need to use the Get-ScsiLun cmdlet:
    $esxihost | Get-ScsiLun
    
  11. This returns a list of disks available to the SCSI subsystem under ESXi. The list might contain a lot of objects. You can use various properties returned by the ScsiLun object to identify and leverage the list for provisioning. For instance, you can scope the list using the Vendor property or by the model. For the purpose of this example, we will assume that you have a disk identified by the iSCSIDisk model and use that for scoping. To create a new datastore on the disk, you need the canonical name, which is also a property in the ScsiLun object:
    $LUN = $esxihost | Get-ScsiLun | Where {$_.Model -like "iSCSIDisk"} 
    
  12. In situations where you have many disks presented to a host, identification by model might not be the best. Another method would be to use the RuntimeName property that enumerates the HBA, controller, target, and the LUN number. For instance, if you know the LUN number you want to prepare is LUN 8 that is represented in the RuntimeName as L8, the PowerCLI to scope and return this would be as follows:
    $LUN = $esxihost | Get-ScsiLun | Where {$_.RuntimeName -like "*L8"} 
    
  13. By storing the LUN in a variable, we can verify the returned value to ensure that you have the correct object and number of objects expected before passing it into the New-Datastore cmdlet:
    $esxihost | New-Datastore -Name iSCSIDatastore1 -Path $LUN.CanonicalName -VMFS
    
  14. This provisions the disk as a VMFS filestore and allows it to be used for VM storage. At this point, you can initiate a rescan on all of the ESXi hosts in the cluster and they will all see the same shared storage.

How it works…

Provisioning datastores in vSphere works differently for each type of SAN storage. NFS is simpler than iSCSI or Fibre Channel and just requires that you connect (or mount) the datastore for use on the host. Software-based iSCSI requires that you do some additional configuration on the host so that it can connect to the target array, but then iSCSI and Fibre Channel both will work in the same way with backend storage LUNs being presented to the host for consumption.

See also

  • The Creating and managing datastore clusters and the Performing Storage vMotion recipes in Chapter 4, Working with Datastores and Datastore Clusters

Configuring syslog settings on an ESXi host

Booting your ESXi from SD or USB flash storage is a common scenario. However, when booting from SD and USB, ESXi does not use that storage for logging. Instead, it keeps the logs in memory, which is nonpersistent. Now that you have established a shared, persistent storage, you can point the ESXi hosts syslog functions to store the logs onto the shared disk so that it can survive a reboot or help you to troubleshoot. Even hosts booting from a local spinning disk might want to redirect their syslog onto a shared SAN drive so that it's accessible from another hosts if one of the hosts fails.

Another common use in enterprises is a centralized syslog server or a third-party log collection and analytics service, such as Splunk. Third-party services offer filters, alarms, search, and other advanced features to add context and value to the logs collected from systems.

This section will cover setting this configuration on an ESXi host.

Getting ready

To work in this section, you will need to open a PowerCLI window, connect to an ESXi host, and populate the $esxihost variable with a VMHost object.

How to do it…

  1. PowerCLI provides the Get-AdvancedConfig cmdlet that lets us peer into the advanced settings of the ESXi host. Even in the GUI, the syslog settings for an ESXi host are set within the Advanced Configuration setting. If you enumerate all of the advanced settings and then scope for items with syslog.global, you will see the settings you want to adjust to set centralize logging:
    $esxihost | Get-AdvancedSetting | Where {$_.Name -like "syslog.global*"}
    
    How to do it…

    The two settings you want to adjust are: logDirUnique that sets a subdirectory for each host in the cluster, and logDir that sets the centralized location.

  2. The logDirUnique setting is an easy one. First, you will need to scope down to retrieve just that setting and then pipe it into the Set-AdvancedSetting cmdlet:
    $esxihost | Get-AdvancedSetting | Where {$_.Name -like "Syslog.global.logDirUnique"} | Set-AdvancedSetting -value $true -Confirm:$false
    
  3. The second directory takes a bit more configuration. The logDir setting is a string that defines a storage path. So in our case, you need to figure out which datastore we're going to locate the syslog files onto. The VMFS datastore is identified as a bracketed name, which is followed by a path name. In the earlier example, you created a datastore called iSCSIDatastore1 and you will now use it as our syslog global directory:
    $esxihost | Get-AdvancedSetting | Where {$_.Name -like "Syslog.global.logDirUnique"} | Set-AdvancedSetting -value "[iSCSIDatastore1] syslog" -Confirm:$false
    

    Alternatively, if you want to direct all log files to a centralized syslog server, you can set this setting, the Syslog.global.logHost value.

  4. To set the syslog host value, you will use the same cmdlet used to set the previous values for syslog, except that you will alter the advanced setting used in the Where statement. The value should be Syslog.global.logHost to locate the correct value to be set:
    $esxihost | Get-AdvancedSetting | Where {$_.Name -like "Syslog.global.logHost"} | Set-AdvancedSetting -value " tcp://syslogserver:514 " -Confirm:$false
    

How it works…

The vSphere Advanced Settings control the syslog functions. There are properties in the advanced settings that control how often and at what frequency to roll the log files, and in this example, where to store the global syslog directory, and whether to make a unique subdirectory for this host's log files.

The Get-AdvancedSetting and Set-AdvancedSetting cmdlets expose and allow us to set these Advanced Settings from PowerCLI.

Setting the global log directory requires the administrator to choose a datastore and a subdirectory on which to create these log files. The format of the path is set by using the bracketed datastore name and then a relative path inside the datastore. This is a path definition that vSphere understands, but it is also specific to vSphere. It uses a Linux-like path definition, but it begins inside the datastore location.

There's more…

In general, it's best to leave vSphere advanced settings with their default values unless instructed to make changes by VMware support. The vSphere advanced settings can alter the behavior of ESXi significantly and should be done with caution.

Joining an ESXi host to vCenter

Joining an ESXi host to vCenter is done from vCenter. The cmdlets for adding a host to a vCenter installation all require communication with vCenter. In this section, we'll connect to vCenter and add the host into inventory. All additional configuration to vCenter from PowerCLI will be covered in the next chapter.

Getting ready

Open a new PowerCLI window. This will ensure that no variables are populated and no open connection to an ESXi is lingering.

How to do it…

In this example, you will connect to a vCenter Server instead of directly connecting to an ESXi host. Our vCenter server has the hostname: vcentersrv.domain.local.

  1. To connect to vCenter, use the same cmdlet that you used in the Connecting to an ESXi host or a vCenter instance recipe:
    $vcenter = connect-viserver vcentersrv.domain.local 
    

    Note

    The same certificate warning might be displayed and you might be prompted to log in if your computer cannot single sign-in to the vCenter instance.

  2. Once connected to vCenter, you can use the Add-VMHost to add the host into inventory:
    Add-VMHost -Server $vcenter -Name esxsrv1.domain.local -Location "Primary"
    

    Note

    For the purpose of this section, the value of -Location is assumed to be a datacenter object already created in vCenter. In the next chapter, you'll see code on how to create this datacenter object.

  3. When prompted, enter the administrative account credentials for the ESXi to perform the join operation.
  4. The host is now added to vCenter Server and can be administered by the server.

How it works…

Joining an ESXi to vCenter is a simple cmdlet to configure and complete. It simply links the ESXi into vCenter so that all of the additional configuration and control will be directed from the vCenter host.

At this point, connecting to the ESXi host will display a message in the GUI clients that shows it's being managed by vCenter and that all changes should be made through vCenter. That is mostly the case from PowerCLI too, but there might be additional times when configuration needs to be made directly against a host. One example would be to change multipathing settings for storage.

See also

  • The Creating a virtual datacenter in vCenter, Creating a cluster and adding ESXi hosts, and Setting cluster advanced features, including HA, DRS, and EVC recipes in Chapter 2, Configuring vCenter and Computing Clusters

Creating a configuration script to set all properties uniformly

In this section, you are going to cover all of the cmdlets that have been covered in this chapter by bringing them together into a single script. This script will allow us to take an array of ESXi hosts identified by either their hostname or IP address and to run the full scripted configuration against them.

In many ways, this PowerCLI script will function much like a Host Profile in vSphere. Host Profiles are a configuration definition that can be created from an existing, configured host and applied on hosts to establish a desired configuration state. If hosts deviate from the configuration, the profile might be reapplied to remediate any undesired changes.

Unfortunately, Host Profiles are only available to customers with Enterprise Plus licensing. However, this PowerCLI solution will work for any vSphere customer with Essentials, Essentials Plus, Standard, or Enterprise licensing.

Getting ready

For this last recipe of the chapter, you'll most likely want to open something such as PowerShell ISE. PowerShell ISE provides you with additional tools to edit larger scripts, color code the cmdlets, and ensure that there are no syntax errors. Alternatively, you might want a text editing tool such as NotePad, NoteTab Light, Sublime Text, or NotePad++.

How to do it…

  1. 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:
    # Script to mass configure ESXi hosts
    # Step 1 - Store credentials for ESXi hosts
    # Step 2 – Set a list of target ESXi hosts and IP settings
    # Step 3 – Write a ForEach loop to iterate through hosts
    # Step 4 – Connect to ESXi host
    # Step 5 – Obtain a VMHost object to use for configuration
    # Step 6 – Join the ESXi system to Active Directory
    # Step 7 – Enable services on the ESXi host & set firewall
    # Step 8 – Configure the network settings
    # Step 9 – Configure NFS & iSCSI settings
    # Step 10 - Join hosts to vCenter
    # Step 11 - Rescan for storage changes
    # Step 12 – Configure persistent syslog storage
    
  2. 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.
    # Step 1 - Store credentials for ESXi hosts
    $esxiCreds = Get-Credential
    

    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.

  3. 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:
    # Step 2 - Set a list of target ESXi hosts and IP settings
    $esxiTargets = "192.168.0.241","192.168.0.242", "192.168.0.243", "192.168.0.244"
    
  4. 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:
    $vMotionNetwork = "192.168.50."
    $storageNetwork = "192.168.100."
    $ftlogNetwork = "192.168.200."
    
  5. 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:
    # Step 3 - Write a ForEach loop to iterate through hosts
    ForEach ($hostname in $esxiTargets) {
    
  6. 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.
  7. 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:
    # Step 4 - Connect to ESXi host
    $connectedHost = connect-viserver $hostname -Credential $esxiCreds
    
    # Step 5 – Obtain a VMHost object to use for configuration
    $esxihost = Get-VMHost $hostname
    
  8. 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:
    # Step 6 – Join the ESXi system to Active Directory
    $esxihost | Get-VMHostAuthentication | 
    Set-VMHostAuthentication -JoinDomain -Domain domain.local -user username -password ***** -Confirm:$false
    
    # Step 7 – Enable services on the ESXi host & set firewall
    $esxihost | Get-VMHostService | where { $_.key -eq "TSM-SSH" } | Set-VMHostService -Policy "On" -Confirm:$false
    
    $esxihost | Get-VMHostService | where { $_.key -eq "TSM-SSH" } | Start-VMHostService -Confirm:$false
    
    # Step 8 – Configure the network settings
    $esxihost | Get-VirtualPortGroup -Name "VM Network" | Remove-VirtualPortGroup –Confirm:$false 
    
  9. 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:
    $esxihost_ipaddress = $esxihost | Get-VMHostNetworkAdapter -name vmk0
    
  10. 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.

    $lastOctet = $esxihost_ipaddress.IP.Split(".")[3]
    

    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.

  11. 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:
    $vmotionIP = $vMotionNetwork + $lastOctet
    $storageIP = $storageNetwork + $lastOctet
    $ftlogIP = $ftlogNetwork + $lastOctet
    
  12. 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.
    $esxihost | New-VMHostNetworkAdapter -PortGroup "VMotion Network" -VirtualSwitch vSwitch0 -IP $vmotionIP -SubnetMask 255.255.255.0 -VMotionEnabled $true
    
    $esxihost | Get-VirtualPortGroup -Name "VMotion Network" | Set-VirtualPortGroup –VlanID 50
    
    # Create new virtual switch for Storage and FT Logging
    $esxihost | New-VirtualSwitch -Name vSwitch1 -Nic vmnic2,vmnic3
    
    # Create vmkernel ports for Storage and FT Logging
    $esxihost | New-VMHostNetworkAdapter -PortGroup "Storage Network" -VirtualSwitch vSwitch1 -IP $storageIP -SubnetMask 255.255.255.0 -VsanTrafficEnabled $true
    
    $esxihost | Get-VirtualPortGroup -Name "Storage Network" | Set-VirtualPortGroup –VlanID 100
    
    $esxihost | New-VMHostNetworkAdapter -PortGroup "FT Logging Network" -VirtualSwitch vSwitch1 -IP $ftlogIP -SubnetMask 255.255.255.0 -FaultToleranceLoggingEnabled $true
    
    $esxihost | Get-VirtualPortGroup -Name "FT Logging Network" | Set-VirtualPortGroup –VlanID 200
    
    # Create new Virtual Switch for Virtual Machines
    $esxihost | New-VirtualSwitch -Name vSwitch2 -Nic vmnic4,vmnic5
    
    # Create Port Groups for Virtual Machines
    New-VirtualPortGroup -Name "Infrastructure Network" -VirtualSwitch vSwitch2 -VLanId 1 -Server $connectedhost
    
    New-VirtualPortGroup -Name "Application Network" -VirtualSwitch vSwitch2 -VLanId 2 -Server $connectedhost
    
    # Step 9 – Configure NFS & iSCSI settings
    # Connect NFS datastore
    $esxihost | New-Datastore -Nfs -Name DataStoreName -Path /data1/export -NfsHost nfsserver.domain.local
    
    # Configure iSCSI Settings
    $esxihost | Get-VMHostStorage | Set-VMHostStorage -SoftwareIScsiEnabled $true$iSCSIhba = $esxihost | Get-VMHostHba -Type iScsi
    
    New-IScsiHbaTarget -IScsiHba $iSCSIhba -Address $target -ChapType Required -ChapName vSphere -ChapPassword Password1
    
    $esxcli = Get-ESXCLI -VMHost $esxihost
    
    $esxcli.iscsi.networkportal.add($iscsihba, $true,"vmk2")
    
  13. 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:
    Disconnect-VIServer -Server $connectedHost -Confirm:$false
    }
    

    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 .

  14. 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:
    $vcenter = connect-viserver vcentersrv.domain.local
    $datacenter = Get-Datacenter "Primary"
    

    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.

  15. Next, you will run through an additional ForEach loop to add the hosts and set their settings in vCenter:
    ForEach ($hostname in $esxTarget) {
    
  16. Now, you are ready to add the host into vCenter from the Joining an ESXi host to vCenter recipe:
    # Step 10 - Join hosts to vCenter
    Add-VMHost -Server $vcenter -Name $hostname l -Location $datacenter -Credential $esxiCreds
    
  17. 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:
    $esxihost = Get-VMHost $hostname
    
  18. For the next few steps, you will pull the host settings related to rescanning for datastores and setting the syslog settings:
    # Step 11 - Rescan for storage changes
    $esxihost | Get-VMHostStorage -RescanAllHBA -RescanVmfs
    
    # Step 12 – Configure persistent syslog storage
    $esxihost | Get-AdvancedSetting | Where {$_.Name -like "Syslog.global.logDirUnique"} | Set-AdvancedSetting -value $true -Confirm:$false
    
    $esxhost | Get-AdvancedSetting | Where {$_.Name -like "logDir"} | Set-AdvancedSetting -value "[iSCSIDatastore1] syslog" -Confirm:$false
    
  19. Finally, you will close the loop with a right curly brace.
    }
    

Note

With connect-viserver, you might have to log in a second time in the script with different credentials to vCenter versus individual hosts. Afterwards, the hosts should be populated into vCenter.

Finally, your settings and desired state should be fully transferred to the ESXi host by the script.

How it works…

In this example, you wrap up all of the code you have developed throughout the chapter. You bring together the pieces of code that achieve specific tasks into a fully scripted configuration that you can apply toward a number of ESXi hosts. The script gives us repeatability, so when you need to extend the cluster with a new ESXi, or when you rebuild the host because you've replaced or upgraded the hardware, you can run this script against it and be back to the same working condition as before replacement.

The basis of the script is a ForEach loop. Because you define the ESXi hosts in an array, you can connect to each of them and run all of the commands and then move the next entry in the array. The script also suppresses confirmation dialogs so that it can continue to issue cmdlets against the host. You also stored the login credentials, which means that you only have to log in once and the script will use the same credentials to connect and configure all of the hosts in the defined array.

Left arrow icon Right arrow icon

Description

If you are a virtualization professional who wants to unleash the power of automation and combat the complexity of sprawling virtual environments, this book is ideal for you. This book will enhance your skills of administering VMware vSphere and vCloud Director with PowerCLI.

What you will learn

  • Configure and administer VMware ESXi hosts, clusters, and vCenter Manage virtual machine configurations and operations Administer datastores, datastore clusters, and virtual machine snapshots Create and configure virtual networking in a vSphere environment Manage cluster features and affinity groups Make custom reports and notifications from vSphere Direct operations in vCloud Director
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 25, 2015
Length: 274 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393724
Vendor :
Microsoft
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Mar 25, 2015
Length: 274 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393724
Vendor :
Microsoft
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 125.97
Learning PowerCLI for VMware VSphere
€37.99
Mastering PowerCLI
€45.99
PowerCLI Cookbook
€41.99
Total 125.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. Configuring the Basic Settings of an ESXi Host with PowerCLI Chevron down icon Chevron up icon
2. Configuring vCenter and Computing Clusters Chevron down icon Chevron up icon
3. Managing Virtual Machines Chevron down icon Chevron up icon
4. Working with Datastores and Datastore Clusters Chevron down icon Chevron up icon
5. Creating and Managing Snapshots Chevron down icon Chevron up icon
6. Managing Resource Pools, Reservations, and Limits for Virtual Machines Chevron down icon Chevron up icon
7. Creating Custom Reports and Notifications for vSphere Chevron down icon Chevron up icon
8. Performing ESXCLI and in-guest Commands from PowerCLI Chevron down icon Chevron up icon
9. Managing DRS and Affinity Groups using PowerCLI Chevron down icon Chevron up icon
10. Working with vCloud Director from PowerCLI Chevron down icon Chevron up icon
A. Setting up and Configuring vCloud Director Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(4 Ratings)
5 star 75%
4 star 25%
3 star 0%
2 star 0%
1 star 0%
Chris Wahl Apr 19, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
One of the more difficult parts of learning PowerCLI - and PowerShell itself - is determining where to get started. I often suggest identifying a project or task that you want to complete, and then figuring out how to do it, because aimlessly fiddling with a language can be frustrating. If you're looking for a library of sample code that walks you through the why and how, I'd suggest picking up the PowerCLI Cookbook written by Philip Sellers.The book is published through Packt Publishing, who's library contains material that wildly varies across the quality spectrum. However, it is obvious that Philip put a large quantity of time, effort, and care into this book and offers over 200 pages of in-depth discussion and examples for automating a wide swath of data center activities.Each walk-through is arranged into a recipe, containing details on what will be required to follow along with the recipe, steps required to build the code, details on why the code is setup the way it is (and how to modify it for your own purposes). The author has frequently added additional reading to the recipes with blog links, documentation, KBs, and other handy resources.Of note, Philip has built a fairly robust set method for balancing share allocations and resource pools using some of my older work as a baseline. Don't worry, he pinged me about it before it made it into the book, but his approach of using a custom attributes (page 140) is refreshing and looks solid. He even writes a proper function whereas my code was just a spitball idea.There's also an entire section on snapshot management (page 107) with the steps necessary to build a snapshot management module (page 129). I've talked about PowerShell modules recently, and it's spectacular to see examples on building custom modules in this book. Philip offers ideas around naming of snapshots, when to quiesce memory, and how to automate / prune the snapshot lifecycle. This is solid stuff.The last thing I'll compliment is the points in the book where Philip goes a bit out of scope of a straight cookbook. For example, on page 78 there's a decent volume of content covering the use of Get-View and how to view properties and methods. That's some pretty advanced stuff, but the writing breaks it down into meaningful and digestible chunks. Having hit the limitation of the native PowerCLI cmdlets frequently, there's definitely a lot of value in knowing how to leverage methods directly.If you're brand new to PowerCLI, or looking to leverage some polished examples to help tickle the noodle on coding ideas, I'd suggest picking up a copy of PowerCLI Cookbook. I read the whole thing over the course of a few plane flights because it was that good.Note: I requested a review copy of the book from the author which was fulfilled by the publisher.
Amazon Verified review Amazon
Earl Waud Apr 06, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a virtualization engineer with 9 years of experience creating automation and administration solutions for large corporations.Last year I was contacted by Packt Publishing and asked if I would be interested in providing technical review services for a new book titled PowerCLI Cookbook. It seemed like an excellent opportunity and I quickly agreed.They sent me a draft copy of the book, one chapter at a time. I would review the chapter content for technical accuracy and test each of the chapter’s code samples and downloadable content, and reply with any corrections, my comments and any feedback. I quickly realized that reading a technical book is a lot different than reviewing a technical book. However I still enjoyed the process as It gave me a chance to use (and expand) my skills in a fun new way.Chapter by chapter, I reviewed the book. I found each chapter to be filled with powerful examples, showing ways to simplify and automate the real tasks that vSphere administrators need to do, the real tasks that I need to do. I finished my review of the last chapter back in early November .Well, Packt has finally published the PowerCLI Cookbook.They wanted to give the author time to validate and update the book’s content to include the vSphere 6 feature set.With a published copy in hand, and based on my detailed review of the book’s content, I can tell you with confidence that Philip Sellers has delivered an excellent resource on PowerCLI, and I highly recommend it to anyone looking for such a resource.The book details examples of dozens of highly useful, real world tasks needed for the day-to-day administration of your vSphere environment. The examples are presented in a way that lets your quickly learn to use and adapt them to your needs.Whether you are new to PowerCLI and looking for a book to help you learn and get started, or you are an experienced PowerShell or PowerCLI user looking for a great reference book to help you automate the administration of your virtual environment, you will find this book to be an excellent choice.
Amazon Verified review Amazon
R. Hudson Apr 09, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I’m glad I purchased this book! I’ve already found several topics that I’ve needed to get into my production routine. The book is put together very well with straight forward approaches covering what I and many administrators need every day for administration. Scripting and automation are life savers (no longer optional, but required). I believe I’ve already recouped my investment by just using the recipes presented and I’ve only just begun.
Amazon Verified review Amazon
Vinay Jun 05, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A great reference book to help you automate the administration of your virtual environment!! Grab it now....
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela