Chapter 4. Managing vSphere Hosts with PowerCLI

In a VMware vSphere environment, the hosts are the working horses. They provide the power on which the virtual machines run. So before you can deploy your virtual machines, you first have to deploy your hosts.

In this chapter, you will learn how to manage your hosts using PowerCLI. We will focus on:

  • Adding hosts to a VMware vCenter Server
  • Enabling and disabling maintenance mode
  • Working with host profiles
  • Working with host services
  • Configuring the host firewall
  • Configuring vSphere Image Builder and Auto Deploy
  • Using esxcli from PowerCLI
  • Removing hosts from a VMware vCenter Server

Adding hosts to a VMware vCenter Server

VMware vCenter Server provides a centralized platform to manage your VMware vSphere environments. With vCenter Server, you can create clusters of hosts with High Availability (HA) and Distributed Resource Scheduler (DRS). In this section, you will learn how to add hosts to a VMware vCenter Server.

Creating a data center

After deploying a new vCenter Server, you cannot add a host to a vCenter Server until you have created a data center. So we will start with creating a data center. To create a data center, you have to use the New-Datacenter cmdlet. This cmdlet has the following syntax:

New-Datacenter [-Location] <VIContainer> [-Name] <String> 
    [-Server <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

The -Location and -Name parameters are required.

When you create a data center, you have to specify a value for the location parameter. This location has to be a folder. You can create the data center in the root of your vCenter Server environment. This root is a folder named Datacenters. You can also create a subfolder in the Datacenters folder and create the data center in this subfolder. It is not possible to create a data center in a folder inside another data center. You can use the following command to find all of the folders that can be used to create data centers in:

PowerCLI C:> Get-Folder -Type Datacenter


    Name                           Type
----                           ----
Datacenters                    Datacenter

The following screenshot shows how you can create a data center in the vSphere Web Client. This is different from PowerCLI because you can specify the vCenter Server as the location in the vSphere Web Client.

Creating a data center

In the following example, we will create a data center named New York in the Datacenters folder. First, we use the Get-Folder cmdlet to get the Datacenters folder. Then, we use the pipeline to pass the Datacenters folder to the -Location parameter of the New-Datacenter cmdlet. The -Location parameter accepts the pipeline input ByValue:

PowerCLI C:> $Datacenter = Get-Folder -Name Datacenters |
>> New-Datacenter -Name "New York"
PowerCLI C:> $Datacenter


    Name
----
New York

Creating a cluster

You probably want to create a cluster in which you place your hosts so that you can take advantage of HA and DRS. In Chapter 8 , Managing High Availability and Clustering, you will learn all about clusters. However, we will show you how to create a default cluster here, so you can make your hosts a member of the cluster.

The following screenshot shows how you can create a cluster in the vSphere Web Client:

Creating a cluster

You have to use the New-Cluster cmdlet to create a cluster. The New-Cluster cmdlet has the following syntax:

New-Cluster [-HARestartPriority <HARestartPriority>] [-HAIsolationResponse <HAIsolationResponse>] [-VMSwapfilePolicy <VMSwapfilePolicy>] [-Name] <String> -Location <VIContainer> [-HAEnabled] [-HAAdmissionControlEnabled] [-HAFailoverLevel <Int32>] [-DrsEnabled] [-DrsMode <DrsMode>] [-DrsAutomationLevel <DrsAutomationLevel>] [-VsanDiskClaimMode <VsanDiskClaimMode>] [-VsanEnabled] [-EVCMode <String>] [-Server <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

To create a default cluster, you only have to specify the name and location. The -Name and -Location parameters are required:

PowerCLI C:> $Cluster = New-Cluster -Name Cluster01 -Location
    $Datacenter
PowerCLI C:> $Cluster


    Name      HAEnabled HAFailoverLevel DrsEnabled DrsAutomationLevel
----      --------- --------------- ---------- ------------------
Cluster01 False     1               False      FullyAutomated

Adding a host

To add a host to a VMware vCenter Server, you need to use the Add-VMHost cmdlet. This cmdlet has the following syntax:

Add-VMHost [-Name] <String> [-Port <Int32>] [-Location] <VIContainer> [-Credential <PSCredential>] [-User <String>] [-Password <String>] [-Force] [-RunAsync] [-Server <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

The -Name and -Location parameters are required.

The following screenshot shows how you can add a host to a cluster in the vSphere Web Client:

Adding a host

While adding a host to a vCenter Server, you have to supply the username and the password for the user you want to use, to authenticate with the host. You can specify the username and password as a string, or you can use the -Credential parameter and pass a PSCredential object created with the Get-Credential cmdlet. You also have to specify the DNS name or IP address of the host and the location where you want to add the host. Let's try to add the host to the cluster we created in the preceding section. The following screenshot will show you the output of the Add-VMHost cmdlet:

Adding a host

As you can see from the error message, the command failed because the host is using a self-signed SSL certificate. You have to give the host a trusted certificate or you can use the -Force parameter to skip the certificate check. Take a look at the following code:

PowerCLI C:> Add-VMHost -Name 192.168.0.133 -Location $Cluster `
>> -User root -Password VMware1! -Force


    Name                 ConnectionState PowerState NumCpu CpuUsageMhz
----                 --------------- ---------- ------ -----------
192.168.0.133        Connected       PoweredOn       2           0

The host is now added to the cluster. You can check this with the following command:

PowerCLI C:> $Cluster | Get-VMHost


    Name                 ConnectionState PowerState NumCpu CpuUsageMhz
----                 --------------- ---------- ------ -----------
192.168.0.133        Connected       PoweredOn       2          49
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.243.184