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
Arrow up icon
GO TO TOP
PowerCLI Cookbook

You're reading from   PowerCLI Cookbook Over 75 step-by-step recipes to put PowerCLI into action for efficient administration of your virtual environment

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784393724
Length 274 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Philip Brandon Sellers Philip Brandon Sellers
Author Profile Icon Philip Brandon Sellers
Philip Brandon Sellers
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

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

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
lock icon The rest of the chapter is locked
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 $19.99/month. Cancel anytime
Banner background image