Book Image

Learning PowerCLI - Second Edition

By : Robert van den Nieuwendijk
Book Image

Learning PowerCLI - Second Edition

By: Robert van den Nieuwendijk

Overview of this book

VMware vSphere PowerCLI, a free extension to Microsoft Windows PowerShell, enables you to automate the management of a VMware vSphere or vCloud environment. This book will show you how to automate your tasks and make your job easier. Starting with an introduction to the basics of PowerCLI, the book will teach you how to manage your vSphere and vCloud infrastructure from the command line. To help you manage a vSphere host overall, you will learn how to manage vSphere ESXi hosts, host profiles, host services, host firewall, and deploy and upgrade ESXi hosts using Image Builder and Auto Deploy. The next chapter will not only teach you how to create datastore and datastore clusters, but you’ll also work with profile-driven and policy-based storage to manage your storage. To create a disaster recovery solution and retrieve information from vRealize Operations, you will learn how to use Site Recovery Manager and vRealize Operations respectively. Towards the end, you’ll see how to use the REST APIs from PowerShell to manage NSX and vRealize Automation and create patch baselines, scan hosts against the baselines for missing patches, and re-mediate hosts. By the end of the book, you will be capable of using the best tool to automate the management and configuration of VMware vSphere.
Table of Contents (22 chapters)
Learning PowerCLI Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Retrieving a list of all of your hosts


Similar to the Get-VM cmdlet, which retrieves your virtual machines, is the Get-VMHost cmdlet, which displays your hosts. The Get-VMHost cmdlet has the following syntax. The first parameter set is the default:

Get-VMHost [[-Name] <String[]>] [-NoRecursion] [-Datastore
    <StorageResource[]>] [-State <VMHostState[]>] [-Location
    <VIContainer[]>]
    [-Tag <Tag[]>] [-Server <VIServer[]>][<CommonParameters>]

The second parameter set is for retrieving hosts connected to specific distributed virtual switches:

Get-VMHost [[-Name] <String[]>] [-DistributedSwitch
    <DistributedSwitch[]>] [-Tag <Tag[]>] [-Server <VIServer[]>]
    [<CommonParameters>]

The third parameter set is for retrieving hosts by virtual machine or resource pool:

Get-VMHost [[-Name] <String[]>] [-NoRecursion] [-VM <VirtualMachine[]>]
    [-ResourcePool <ResourcePool[]>] [-Datastore <StorageResource[]>]
    [-Location <VIContainer[]>] [-Tag<Tag[]>] [-Server <VIServer[]>] 
    [<CommonParameters>]

The fourth parameter set is for retrieving hosts by ID:

Get-VMHost [-Server <VIServer[]>] -Id <String[]> [<CommonParameters>]

The -Id parameter is required. The fifth parameter set is for retrieving hosts by related object:

Get-VMHost [-RelatedObject] <VMHostRelatedObjectBase[]>
    [<CommonParameters>]

The -RelatedObject parameter is required.

Don't mix parameters from different sets or you will get an error as follows:

PowerCLI C:\> Get-VMHost -Id HostSystem-host-22 -Name 192.168.0.133
Get-VMHost : Parameter set cannot be resolved using the specified named
    parameters.
At line:1 char:1
+ Get-VMHost -Id HostSystem-host-22 -Name 192.168.0.133
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VMHost], 
                                  ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,
                                  VMware.VimAutomation
                                  .ViCore.Cmdlets.Commands.GetVMHost

To get a list of all of your hosts, type the following command:

PowerCLI C:\> Get-VMHost

By default, only the Name, ConnectionState, PowerState, NumCPU, CpuUsageMhz, CpuTotalMhz, MemoryUsageGB, MemoryTotalGB, and Version properties are shown. To get a list of all of the properties, type the following command:

PowerCLI C:\> Get-VMHost | Format-List -Property *

The output of this command can be seen in the following screenshot:

You can use the Get-VMHost parameters or the Where-Object cmdlet to filter the hosts you want to display, as we did with the Get-VM cmdlet.

Displaying the output in a grid view

Instead of displaying the output of your PowerCLI commands in the PowerCLI console, you can also display the output in a grid view. A grid view is a popup that looks like a spreadsheet with rows and columns. To display the output of the Get-VMHost cmdlet in a grid view, type the following command:

PowerCLI C:\> Get-VMHost | Out-GridView

The preceding command opens the window of the following screenshot:

You can create filters to display only certain rows, and you can sort columns by clicking on the column header. You can also reorder columns by dragging and dropping them. In the following screenshot, we created a filter to show only the hosts with a CpuUsageMhz value greater than or equal to 22. We also changed the order of the ConnectionState and PowerState columns.

Isn't that cool?