Learning Basic PowerCLI Concepts

0
1409
6 min read

In this article, by Robert van den Nieuwendijk, author of the book Learning PowerCLI – Second Edition, you will learn some basic PowerShell and PowerCLI concepts. Knowing these concepts will make it easier for you to learn the advanced topics. We will cover the Get-Command, Get-Help, and Get-Member cmdlets in this article.

(For more resources related to this topic, see here.)

Using the Get-Command, Get-Help, and Get-Member cmdlets

There are some PowerShell cmdlets that everyone should know. Knowing these cmdlets will help you discover other cmdlets, their functions, parameters, and returned objects.

Using Get-Command

The first cmdlet that you should know is Get-Command. This cmdlet returns all the commands that are installed on your computer. The Get-Command cmdlet has the following syntax:


Get-Command [[-ArgumentList] <Object[]>] [-All] [-ListImported] [-Module <String[]>] [-Noun <String[]>] [-ParameterName <String[]>] [-ParameterType <PSTypeName[]>] [-Syntax] [-TotalCount <Int32>] [-Verb <String[]>] [<CommonParameters>]

Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-All] [-CommandType <CommandTypes>] [-ListImported] [-Module <String[]>] [-ParameterName <String[]>] [-ParameterType <PSTypeName[]>] [-Syntax] [-TotalCount <Int32>] [<CommonParameters>]

The first parameter set is named CmdletSet, and the second parameter set is named AllCommandSet.

If you type the following command, you will get a list of commands installed on your computer, including cmdlets, aliases, functions, workflows, filters, scripts, and applications:

PowerCLI C:> Get-Command

You can also specify the name of a specific cmdlet to get information about that cmdlet, as shown in the following command:

PowerCLI C:> Get-Command –Name Get-VM

This will return the following information about the Get-VM cmdlet:

CommandType Name   ModuleName

----------- ----   ----------

Cmdlet     Get-VM VMware.VimAutomation.Core

You see that the command returns the command type and the name of the module that contains the Get-VM cmdlet. CommandType, Name, and ModuleName are the properties that the Get-VM cmdlet returns by default. You will get more properties if you pipe the output to the Format-List cmdlet. The following screenshot will show you the output of the Get-Command –Name Get-VM | Format-List * command:

You can use the Get-Command cmdlet to search for cmdlets. For example, if necessary, search for the cmdlets that are used for vSphere hosts. Type the following command:

PowerCLI C:> Get-Command -Name *VMHost*

If you are searching for the cmdlets to work with networks, use the following command:

PowerCLI C:> Get-Command -Name *network*

Using Get-VICommand

PowerCLI has a Get-VICommand cmdlet that is similar to the Get-Command cmdlet. The Get-VICommand cmdlet is actually a function that creates a filter on the Get-Command output, and it returns only PowerCLI commands. Type the following command to list all the PowerCLI commands:

PowerCLI C:> Get-VICommand

The Get-VICommand cmdlet has only one parameter –Name. So, you can also type, for example, the following command to get information only about the Get-VM cmdlet:

PowerCLI C:> Get-VICommand –Name Get-VM

Using Get-Help

To discover more information about cmdlets, you can use the Get-Help cmdlet. For example:

PowerCLI C:> Get-Help Get-VM

This will display the following information about the Get-VM cmdlet:

The Get-Help cmdlet has some parameters that you can use to get more information. The –Examples parameter shows examples of the cmdlet. The –Detailed parameter adds parameter descriptions and examples to the basic help display. The –Full parameter displays all the information available about the cmdlet. And the –Online parameter retrieves online help information available about the cmdlet and displays it in a web browser. Since PowerShell V3, there is a new Get-Help parameter –ShowWindow. This displays the output of Get-Help in a new window. The Get-Help -ShowWindow command opens the following screenshot:

Using Get-PowerCLIHelp

The PowerCLI Get-PowerCLIHelp cmdlet opens a separate help window for PowerCLI cmdlets, PowerCLI objects, and articles. This is a very useful tool if you want to browse through the PowerCLI cmdlets or PowerCLI objects. The following screenshot shows the window opened by the Get-PowerCLIHelp cmdlet:

Using Get-PowerCLICommunity

If you have a question about PowerCLI and you cannot find the answer in this article, use the Get-PowerCLICommunity cmdlet to open the VMware vSphere PowerCLI section of the VMware VMTN Communities. You can log in to the VMware VMTN Communities using the same My VMware account that you used to download PowerCLI. First, search the community for an answer to your question. If you still cannot find the answer, go to the Discussions tab and ask your question by clicking on the Start a Discussion button, as shown later. You might receive an answer to your question in a few minutes.

Using Get-Member

In PowerCLI, you work with objects. Even a string is an object. An object contains properties and methods, which are called members in PowerShell. To see which members an object contains, you can use the Get-Member cmdlet. To see the members of a string, type the following command:

PowerCLI C:> "Learning PowerCLI" | Get-Member

Pipe an instance of a PowerCLI object to Get-Member to retrieve the members of that PowerCLI object. For example, to see the members of a virtual machine object, you can use the following command:

PowerCLI C:> Get-VM | Get-Member

   TypeName:

VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl

Name                   MemberType Definition

----                   ---------- ----------

ConvertToVersion       Method     T VersionedObjectInterop.Conver...

Equals                 Method     bool Equals(System.Object obj)

GetConnectionParameters Method     VMware.VimAutomation.ViCore.Int...

GetHashCode             Method     int GetHashCode()

GetType                 Method     type GetType()

IsConvertableTo         Method     bool VersionedObjectInterop.IsC...

LockUpdates             Method     void ExtensionData.LockUpdates()

ObtainExportLease       Method     VMware.Vim.ManagedObjectReferen...

ToString               Method     string ToString()

UnlockUpdates           Method     void ExtensionData.UnlockUpdates()

CDDrives               Property   VMware.VimAutomation.ViCore.Typ...

Client                 Property   VMware.VimAutomation.ViCore.Int...

CustomFields           Property   System.Collections.Generic.IDic...

DatastoreIdList         Property   string[] DatastoreIdList {get;}

Description             Property   string Description {get;}

DrsAutomationLevel     Property   System.Nullable[VMware.VimAutom...

ExtensionData           Property   System.Object ExtensionData {get;}

FloppyDrives           Property   VMware.VimAutomation.ViCore.Typ...

Folder                 Property   VMware.VimAutomation.ViCore.Typ...

FolderId               Property   string FolderId {get;}

Guest                   Property   VMware.VimAutomation.ViCore.Typ...

GuestId                 Property   string GuestId {get;}

HAIsolationResponse     Property   System.Nullable[VMware.VimAutom...

HardDisks               Property   VMware.VimAutomation.ViCore.Typ...

HARestartPriority       Property   System.Nullable[VMware.VimAutom...

Host                   Property   VMware.VimAutomation.ViCore.Typ...

HostId                  Property   string HostId {get;}

Id                     Property   string Id {get;}

MemoryGB               Property   decimal MemoryGB {get;}

MemoryMB               Property   decimal MemoryMB {get;}

Name                   Property   string Name {get;}

NetworkAdapters         Property   VMware.VimAutomation.ViCore.Typ...

Notes                   Property   string Notes {get;}

NumCpu                 Property   int NumCpu {get;}

PersistentId           Property   string PersistentId {get;}

PowerState             Property   VMware.VimAutomation.ViCore.Typ...

ProvisionedSpaceGB     Property   decimal ProvisionedSpaceGB {get;}

ResourcePool           Property   VMware.VimAutomation.ViCore.Typ...

ResourcePoolId         Property   string ResourcePoolId {get;}

Uid                     Property   string Uid {get;}

UsbDevices             Property   VMware.VimAutomation.ViCore.Typ...

UsedSpaceGB             Property   decimal UsedSpaceGB {get;}

VApp                   Property   VMware.VimAutomation.ViCore.Typ...

Version                 Property   VMware.VimAutomation.ViCore.Typ...

VMHost                 Property   VMware.VimAutomation.ViCore.Typ...

VMHostId               Property   string VMHostId {get;}

VMResourceConfiguration Property   VMware.VimAutomation.ViCore.Typ...

VMSwapfilePolicy       Property   System.Nullable[VMware.VimAutom...

The command returns the full type name of the VirtualMachineImpl object and all its methods and properties.

Remember that the properties are objects themselves. You can also use Get-Member to get the members of the properties. For example, the following command line will give you the members of the VMGuestImpl object:

PowerCLI C:> $VM = Get-VM –Name vCenter

PowerCLI C:> $VM.Guest | Get-Member

Summary

In this article, you looked at the Get-Help, Get-Command, and Get-Member cmdlets.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here