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 2.  Learning Basic PowerCLI Concepts

While learning something new, you always have to learn the basics first. In this chapter, 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 following topics in this chapter:

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

  • Using providers and PSdrives

  • Using arrays and hash tables

  • Creating calculated properties

  • Using raw API objects with ExtensionData or Get-View

  • Extending PowerCLI objects with the New-VIProperty cmdlet

  • Working with vSphere folders

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. The first parameter set is named CmdletSet:

Get-Command [[-Name] <String[]>] [[-ArgumentList] <Object[]>] [-All] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | Workflow | Configuration | All}] [-FullyQualifiedModule <ModuleSpecification[]>] [-ListImported] [-Module <String[]>] [-ParameterName <String[]>] [-ParameterType <PSTypeName[]>] [-ShowCommandInfo] [-Syntax] [-TotalCount <Int32>] [<CommonParameters>]

The second parameter set is named AllCommandSet:

Get-Command [[-ArgumentList]...

Using providers and PSDrives


Until now, you have only seen cmdlets. Cmdlets are PowerShell commands. PowerShell has another import concept named providers. Providers are accessed through named drives or PSDrives. In the following sections, Using providers and Using PSDrives, providers and PSDrives will be explained.

Using providers

A PowerShell provider is a piece of software that makes datastores look like filesystems. PowerShell providers are usually part of a snap-in or a module-like PowerCLI. The advantage of providers is that you can use the same cmdlets for all the providers. These cmdlets have the following nouns: Item, ChildItem, Content, and ItemProperty. You can use the Get-Command cmdlet to get a list of all the cmdlets with these nouns:

PowerCLI C:> Get-Command -Noun Item,ChildItem,Content,ItemProperty |
Format-Table -AutoSize

The preceding command gives the following output:

CommandType Name                Version Source
----------- ----                ------- ------
Cmdlet...

Using arrays and hash tables


In PowerCLI, you can create a list of objects. For example, red, white, and blue is a list of strings. In PowerShell, a list of terms is named an array. An array can have zero or more objects. You can create an empty array and assign it to a variable:

PowerCLI C:\> $Array = @()

You can fill the array during creation using the following command line:

PowerCLI C:\> $Array = @("red","white")

You can use the += operator to add an element to an array:

PowerCLI C:\> $Array += "blue"
PowerCLI C:\> $Array
red
white
blue

If you want to retrieve a specific element of an array, you can use an index starting with 0 for the first element, 1 for the second element, and so on. If you want to retrieve an element from the tail of the array, you have to use -1 for the last element, -2 for the second to last, and so on. You have to use square brackets around the index number. In the next example, the first element of the array is retrieved using the following command...

Creating calculated properties


You can use the Select-Object cmdlet to select certain properties of the objects that you want to return. For example, you can use the following code to return the name and the used space, in GB, of your virtual machines:

PowerCLI C:\> Get-VM | Select-Object -Property Name,UsedSpaceGB

But what if you want to return the used space in MB? The PowerCLI VirtualMachineImpl object has no UsedSpaceMB property. This is where you can use a calculated property. A calculated property is a PowerShell hash table with two elements: Name and Expression. The Name element contains the name that you want to give the calculated property. The Expression element contains a scriptblock with PowerCLI code to calculate the value of the property. To return the name and the used space in MB for all your virtual machines, run the following command:

PowerCLI C:\> Get-VM |
>> Select-Object -Property Name,
>> @{Name="UsedSpaceMB";Expression={1KB*$_.UsedSpaceGB}}
>>...

Using raw API objects with ExtensionData or Get-View


PowerCLI makes it easy to use the VMware vSphere application programming interface (API). There are two ways to do this. The first one is by using the ExtensionData property that most of the PowerCLI objects have. The ExtensionData property is a direct link to the vSphere API object related to the PowerCLI object. The second way is by using the Get-View cmdlet to retrieve the vSphere API object related to a PowerCLI object. Both these ways will be discussed in the following sections.

Using the ExtensionData property

Most PowerCLI objects, such as VirtualMachineImpl and VMHostImpl, have a property named ExtensionData. This property is a reference to a view of a VMware vSphere object as described in the VMware vSphere API Reference documentation. For example, the ExtensionData property of the PowerCLI's VirtualMachineImpl object links to a vSphere VirtualMachine object view. ExtensionData is a very powerful property because it allows you to...

Extending PowerCLI objects with the New-VIProperty cmdlet


Sometimes, you can have the feeling that a PowerCLI object is missing a property. Although the VMware PowerCLI team tried to include the most useful properties in the objects, you can have the need for an extra property. Luckily, PowerCLI has a way to extend a PowerCLI object using the New-VIProperty cmdlet. This cmdlet has the following syntax:

New-VIProperty [-Name] <String> [-ObjectType] <String[]> [-Value] 
    <ScriptBlock> [-Force] [-BasedOnExtensionProperty <String[]>] [-WhatIf]
    [-Confirm] [<CommonParameters>]
New-VIProperty [-Name] <String> [-ObjectType] <String[]> [-Force]
    [-ValueFromExtensionProperty] <String> [-WhatIf] [-Confirm]
    [<CommonParameters>]

Let's start with an example. You will add the VMware Tools' running statuses used in a previous example to the VirtualMachineImpl object using the New-VIProperty cmdlet:

PowerCLI C:\> New-VIProperty -ObjectType...

Working with vSphere folders


In a VMware vSphere environment, you can use folders to organize your infrastructure. In the vSphere web client, you can create folders in the Hosts and Clusters, VMs and Templates, Storage, and Networking inventories. The following screenshot shows an example of folders in the VMs and Templates inventory:

You can browse through these folders using the PowerCLI Inventory Provider. PowerCLI also has a set of cmdlets to work with these folders: Get-Folder, Move-Folder, New-Folder, Remove-Folder, and Set-Folder.

You can use the Get-Folder cmdlet to get a list of all of your folders:

PowerCLI C:\> Get-Folder

Otherwise, you can select specific folders by their name using the following command line:

PowerCLI C:\> Get-Folder -Name "Accounting"

All folders are organized in a tree structure under the root folder. You can retrieve the root folder with the following command:

PowerCLI C:\> Get-Folder -NoRecursion


    Name                           Type
----  ...

Summary


In this chapter, you looked at the Get-Help, Get-Command, and Get-Member cmdlets. You learned how to use providers and PSDrives. You also saw how to create a calculated property. Using the raw API objects with the ExtensionData property or the Get-View cmdlet was discussed, and you looked at extending PowerCLI objects with the New-VIProperty cmdlet. At the end, you learned to work with folders, and you saw how you can use the New-VIProperty cmdlet to extend the Folder object of PowerCLI with a Path property.

In the next chapter, you will learn more about working with objects in 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