Book Image

PowerCLI Cookbook

By : Philip Brandon Sellers
Book Image

PowerCLI Cookbook

By: Philip Brandon Sellers

Overview of this book

Table of Contents (19 chapters)
PowerCLI Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
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:

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