Reader small image

You're reading from  Learning PowerCLI - Second Edition

Product typeBook
Published inFeb 2017
Publisher
ISBN-139781786468017
Edition2nd Edition
Right arrow
Author (1)
Robert van den Nieuwendijk
Robert van den Nieuwendijk
author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk

Right arrow

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...

Enabling and disabling maintenance mode


If you want to shut down, patch, upgrade, or reconfigure a host, you do not want any virtual machines running on the host. If you put a host in maintenance mode, you are sure that no virtual machines will be moved to or started on the host. If the host is running on a fully automated DRS-enabled cluster, the DRS will move the running virtual machines from the host to other hosts in the cluster using vMotion.

The following screenshot of the vSphere Web Client shows the different DRS automation levels that a cluster can have:

To put a host in maintenance mode, you have to use the Set-VMHost cmdlet.

This cmdlet has the following syntax:

Set-VMHost [-VMHost] <VMHost[]> [[-State] <VMHostState>] [-VMSwapfilePolicy <VMSwapfilePolicy>] [-VMSwapfileDatastore <Datastore>] [-Profile <VMHostProfile>] [-Evacuate] [-TimeZone <VMHostTimeZone>] [-LicenseKey <String>] [-VsanDataMigrationMode <VsanDataMigrationMode>] [...

Working with host profiles


A host profile is a collection of all of the configuration settings for an ESXi host, such as storage and networking configurations and security settings. You can create a host profile from a reference host or import an existing host profile. After attaching a host profile to a host, the host can be checked for compliance with the host profile. If the host is compliant, you know the settings of the host are the same as the settings of the host profile. If the host is not compliant, the host profile can be applied to the host to make the host compliant.

The following screenshot of the vSphere Web Client shows you some of the settings that you can configure in a host profile:

Common Information Model (CIM) indication subscriptions are subscriptions to notifications for hardware-related events, such as problems with the cooling, battery, processor, memory, or power of an ESXi server.

Creating a host profile

To get started, you first need to configure a reference host...

Working with host services


PowerCLI has some cmdlets to work with host services. You can easily find these cmdlets using the Get-Command cmdlet, as follows:

PowerCLI C:\> Get-Command -Noun VMHostService

The preceding command has the following output:

CommandType Name                  ModuleName
----------- ----                  ----------
Cmdlet      Get-VMHostService     VMware.VimAutomation.Core
Cmdlet      Restart-VMHostService VMware.VimAutomation.Core
Cmdlet      Set-VMHostService     VMware.VimAutomation.Core
Cmdlet      Start-VMHostService   VMware.VimAutomation.Core
Cmdlet      Stop-VMHostService    VMware.VimAutomation.Core

In the following sections, we will discus the *-VMHostService cmdlets.

Retrieving information about host services

You can use the Get-VMHostService cmdlet to retrieve information about the services running on a host.

The syntax of the Get-VMHostService cmdlet is:

Get-VMHostService [-VMHost] <VMHost[]> [-Refresh] [-Server
    <VIServer[]>] [<...

Configuring the host firewall


A host firewall is a VMware vSphere feature to protect the host against attacks. The host firewall allows or blocks traffic to and from specific host services. You can use PowerCLI to configure the host firewall.

To get a list of all of the PowerCLI cmdlets that you can use to configure a host firewall, type the following command:

PowerCLI C:\> Get-Command -Noun VMHostFirewall*


    CommandType Name                            ModuleName
----------- ----                            ----------
Cmdlet      Get-VMHostFirewallDefaultPolicy VMware.VimAutomation.Core
Cmdlet      Get-VMHostFirewallException     VMware.VimAutomation.Core
Cmdlet      Set-VMHostFirewallDefaultPolicy VMware.VimAutomation.Core
Cmdlet      Set-VMHostFirewallException     VMware.VimAutomation.Core

Getting the host firewall default policy

To get the host firewall default policy of a host, you have to use the Get-VMHostFirewallDefaultPolicy cmdlet.

The syntax of the Get-VMHostFirewallDefaultPolicy...

Using vSphere Image Builder and Auto Deploy


VMware vSphere Auto Deploy is a cool feature introduced in VMware vSphere 5.0 that enables you to provide physical hosts with the ESXi software via the PXE boot instead of installing an ESXi server from a CD-ROM. Auto Deploy can be useful if you need to deploy many ESXi servers. If you only have a few ESXi servers, Auto Deploy is probably not the best solution for you because of the amount of work you have to do to set up Auto Deploy. To enable Auto Deploy, you need a vCenter Server, an Auto Deploy server, a DNS server, a DHCP server, a TFTP server, and PowerCLI. Yes, you have seen it right. PowerCLI is a core component of Auto Deploy. All of the information you need to enable and configure Auto Deploy can be found in the VMware document, vSphere Installation and Setup Guide. You can download this document from the VMware documentation page at https://www.vmware.com/support/pubs/ .

Note

Because this is a PowerCLI book, we will focus on the PowerCLI...

Using esxcli from PowerCLI


VMware offers more command-line interfaces for vSphere than PowerCLI. One of them is the vSphere Command-Line Interface (CLI). The vSphere CLI has a command named esxcli. PowerCLI has built-in support for this esxcli command in the Get-EsxCli cmdlet.

Note

There are no New-EsxCli, Set-EsxCli and Remove-EsxCli cmdlets. The Get-EsxCli cmdlet exposes the esxcli functionality for a host. You cannot create a new one, modify, or remove it.

The syntax of the Get-EsxCli cmdlet is as follows:

Get-EsxCli -VMHost <VMHost[]> [-V2] [[-Server] <VIServer[]>] 
    [<CommonParameters>]

Use the Get-EsxCli cmdlet to connect to the esxcli functionality of a host and save the connection in a variable $esxcli:

PowerCLI C:\> $esxcli = Get-EsxCli -VMHost 192.168.0.133

In the vSphere CLI, the command to get information about the CPUs in your host is:

C:\>esxcli --server=192.168.0.133 hardware cpu list
Enter username: root
Enter password:

In PowerCLI, the command...

Removing hosts from a VMware vCenter Server


To remove a host from your vCenter Server inventory, you have to use the Remove-VMHost cmdlet. The Remove-VMHost cmdlet has the following syntax:

Remove-VMHost [-VMHost] <VMHost[]> [-Server <VIServer[]>] 
    [-WhatIf] [-Confirm] [<CommonParameters>]

Let's try to remove a host, as seen in the following screenshot:

.

The operation failed because the host must be in maintenance mode or disconnected state before you can remove it, as you can see in the following screenshot from the vSphere Web Client:

So, let's put the host in maintenance mode first and then try to remove it:

PowerCLI C:\> $VMHost = Get-VMHost -Name 192.168.0.133
PowerCLI C:\> $VMHost | Set-VMHost -State Maintenance


    Name                 ConnectionState PowerState NumCpu CpuUsageMhz
----                 --------------- ---------- ------ -----------
192.168.0.133        Maintenance     PoweredOn       2          41


    PowerCLI...

Summary


In this chapter, we covered working with ESXi hosts using PowerCLI. You saw how to add and remove hosts to and from your vSphere vCenter Server inventory. We looked at putting a host in maintenance mode and how to exit maintenance mode. We have seen how PowerCLI commands work with host profiles and host services. You learned to use the PowerCLI commands for Image Builder and Auto Deploy, and finally, the use of the esxcli command from PowerCLI was discussed. The next chapter will be about managing virtual machines with PowerCLI.

lock icon
The rest of the chapter is locked

Unlock this book and the full library FREE for 7 days

Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
You have been reading a chapter from
Learning PowerCLI - Second Edition
Published in: Feb 2017Publisher: ISBN-13: 9781786468017
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at&nbsp;http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk