PowerCLI study guide – core concepts

This course will introduce the core concepts of PowerCLI.

Being proficient in “PowerCLI” could be summarised in this way:
70% Know PowerShell core concepts relevant for PowerCLI
30% Understand the concepts specifics to PowerCLI
And as prerequisite, understand VMware technologies

The focus of this study guide will be on these 30%.
Each exercise has been designed to teach you one PowerCLI specific skill.
The other 70% will be covered in the PowerShell study guide – core concepts

Scope of this course

This is not a PowerShell course
However you will need PowerShell skills to complete many tasks.
If you don’t have PowerShell skills yet feel free to start by the following PowerShell study guide – core concepts
It contains only PowerShell core concepts relevant for PowerCLI.
For example, you will not need to learn how to execute command on a remote Windows computer to use PowerCLI.

Make you “proficient” in the PowerCLI language
It does not mean to know everything but instead to know the “logic” to be able to handle any challenge, in others words:
able to reuse and adapt existing scripts
able to create a script from scratch when needed

It will only focus on PowerCLI and API reachable via PowerCLI
For example, the areas below will be out of scope.
Manage some VMware products via PowerShell and Rest API that are not yet manageable via PowerCLI.
Connect to the CIM interface of an ESXi host via PowerShell.

It will not cover all areas of PowerCLI
There are just too many areas, with thousands of functions when using the APIs.
The goal is to teach you how to “think PowerCLI” then you will be able to handle any challenges.

This tutorial is not a self-standing course or a list of recipes
You will need to practice to benefit from it.
If you prefer to study offline, I would recommend the books in the appendix.

Only focus on PowerCLI on Windows
PowerCLI on LINUX is consequently out of scope.

PowerCli in context

PowerCLI in context
The above diagram is extracted from “PowerCLI study guide from rookie to guru
PowerCLI could be considered as an “extension” from PowerShell and PowerShell is built on top of “.Net”
This is why you need to learn PowerShell and “.net” in order to make the most out of PowerCLI

The VMware.Vim.dll is quite critical
Backward compatibility for VMware.Vim.dll

Documentation

You will often use these links so add them as bookmarks
VMware vSphere PowerCLI Cmdlets Reference
VMware vSphere API Reference

The official PowwerCLI User’s guide contains many useful information
VMware vSphere PowerCLI User’s Guide or PDF

List of exercises

Pre-requisite

Windows workstation
Ideally with latest PowerShell version (5.0)
Latest version of PowerCLI installed with all optional modules. (6.3 Release 1 at this stage)
Able to connect from the workstation to a VMware test environment based on vCenter 6, multiples hosts and shared storage. (It can be nested hypervisors)

How to handle each exercise

Try alone initially
Try to find a solution using the official documentation, Onyx – see appendix, and your test environment.
This is the best way to learn, because it will teach you how to handle a challenge from scratch.
The goal of this tutorial is not to find an answer, but to learn how to “think PowerCLI” to find an answer.

Use the clue if needed
The clue will point you in the right direction then try again alone.
Please note that not all topics will have a clue.

Search a solution online if needed
Some topics are really challenging and maybe someone has already done the job for you.

Check the Answer
This is not necessary “THE” answer.
There are many ways to achieve the same result with PowerCLI and the criteria for a good script will differ from one person to another.
For example, maybe someone will prefer a script with high performance using API and parallel task at a cost of increased complexity.

Read extra information
Finally more information about the concepts associated to each exercise complement the answer.

Start PowerCLI for the first time

Update the PowerShell execution policy if needed.
List all commands specifics to PowerCLI
Keep only the command name, export result as a GridView
Then count how many command are available.

Understand PowerCLI Modules and Snapins

Launch PowerShell
Import all PowerCLI Modules and Snapin
Then count how many PowerCLI command are available.

Connect to vCenter / Use Credential Store / Undestand $DefaultVIserver

Connect to vCenter using windows account if the workstation is one the the same AD domain
Connect to vcenter using User and Password
Connect to vCenter and save password in the credential store
Connect to vCenter and use the credentials in the credential store
Check the content of $Global:DefaultViServer
Check the content of $Global:DefaultViServers
Disconnect from all vcenter
Add credentials in the credential store
List all credentials in the credential store

Notes: You can also connect to an ESXi host to test using credentials

Extract information about ESXi hosts

Extract the following information about all ESXI hosts connected to vcenter and export it as a CSV file.
Name
State
Model
Version
Build

Use .extensiondata and Performance test

This time export for each host, the name, bios version AND the BIOS Release Date using get-vmhost and the property “.extensiondata”
Finally monitor the execution time of this command.

New-viproperty and Performance test

This time you will need to get the Name,Bios Version and Bios release date using the following command:
get-vmhost | select name,BiosVersion,ReleaseDate
By default the two Bios related properties are not available for a VMHost object so you will need to use the New-VIProperty
Then measure the execution time of this command.

Get-View and Get-VIObjectByVIView

Use get-vmhost, and store the first VMhost object, actually a VIObject, in one Variable called
$VMHOST.
get the type of this object
Display all properties

Then use get-view to get the “View” that correspond to this above PowerCLI object and store it in a variable $VMHOSTView
get the type of this object
Show all properties

Then use get-VIOBbjectByVIView to get the VIObject associated to the “view” object $VMHOSTView object and store it in a variable $VMhostVIObject
get the type of this object
Show all properties

Get-view and Performance test

Export the name of all ESXi hosts using get-vmhost
Export the name of all ESXi using get-view
Compare the execution time of these two commands

Then
Export the name, Version and Build of all hosts using get-vmhost
Export the name, Version and Build of all hosts using get-view
Compare the execution time of these two commands

Managed Object Reference

Export the property moref of the first ESXi host in the environment
Note: This property is available in the “View” object.
Notice the two properties that makes Moref.
Get all members of the Moref property and check the type.
Find it in the official documentation

As you can see there are no really useful methods for this object.
This class is used to refer to a server-side Managed Object.

Then select all properties in the ConfigManager “View” property of the first host.
Then get all members of the subproperty CPUScheduler
“Déjà Vu” isn’t it?
Then get the “view” object associated to this “CPUScheduler” Managed Object Reference and store it in a variable $CPUSchedulerView”
Get all members associated to this $CPUSchedulerView and check the type.
Find it in the official documentation

Modify ESXi host advanced settings using PowerCLI cmdlets

Time to perform some changes…in your test environment.
Select the first ESXI host in your environment
Modify the settings “UserVars.SuppressShellWarning” from the default value 0 to 1 with PowerCLI cmdlets.
Then restore settings to the default value.

Modify advanced settings using API

Same question as the previous exercise…but this time let’s imagine that the PowerCLI team didn’t create cmdlet to configure advanced settings.
A scenario like this one will happen often, especially when new features are introduced in VMware.
In that case you will have to rely on the API…
You already know two ways to access the API properties, this time you will need to access API methods.

In this scenario, it is totally fine to “cheat” and use “onyx”.

Create VM using PowerCLI

Assumption you have at least one VMhost / ESXi not part of a cluster in your inventory.

This host has access to a datastore with enough space.
In my lab it is “10.0.0.11” and “HDD2”

Using only “PowerCLI cmdlets” perform the following task.
Create a VM “testVMPowerCLI” with default settings on the first ESXi host.
Add a disk of “10GB” to “testVMPowerCLI”

Create VM using API

This time create the VM using the API. (In others words without using “new-vm”)
The host has access to a datastore with enough space.
In my lab it is “10.0.0.11” and “HDD2”

Create a VM “testVMAPI” in one operation:
With the bare minimum settings needed to create a VM with the exception of rules below.
Configure the latency Sensitivity to “High”
Add a “VMware paravirtual” controller
Add a disk of “10GB thin” added to this “paravirtual” controller
Finally the VM will need to be added in “VMs and Templates view” under the datacenter where is located the ESXi host.

Note: If you have the choice between two methods with the same name but one of them end by “_Task” use the one without “_Task”


Understand the difference between “View” methods with or without “_task”

Get the “View” object of the virtual machine folder of the datacenter that you have used in the previous exercise and identify all methods available for this object.
Create two new VMs with the API using only the bare minimum settings (So the most simple VM without disks/Network etc)

Create a VM “VM1WithoutTask” using the method “CreateVM”
Create a VM “VM2WithTask” using the method “CreateVM_Task”
Perform the same tasks for two new VMs but this time sort the result in a variable and get the type of each variable.
“VM3WithoutTask” in variable “VM3WithoutTask”
“VM4WithTask” in variable “VM4WithTask”

Then get the Managed Object View associated to the above moref and check the type.

Find VM with a specific IP

I have seen this question often in the PowerCLI community.
Goal: find which vm(s) have a specific guest OS IP.
Assumption:
VMs have VMware tools installed and so you can see the guest OS IP from vCenter
Try to find the fastest way to get it with PowerCLI or the API

ServiceInstance probably the most critical ManagedObject

You must be connected to one and only one vCenter
Get the serviceinstance View object using get-view and store it in a variable $ServiceInstanceView
Explore the members and properties of $ServiceInstanceView

Explore the property of
$global:DefaultViServer
And
($global:DefaultViServer).extensiondata

Store the ServiceContent Object in a variable $ServiceContentView
Explore all members and check the official documentation for this object.
Create a new IP pool in the datacenter where you have created your VMs
Name test20
Subnet 10.20.20.0 /24
Gateway 10.20.20.1
Enable IP Pool
Range 10.20.20.10#15

Extract events with with PowerCLI cmdlet

We have already created VM in previous tasks.
Extract all events “Created virtual machine” using PowerCLI

Extract events with API

Extract all events “Created virtual machine” using API

Execute ESXCLI command from PowerCLI

Find the PowerCLI command that provide access to ESXCLI.
Select the first ESXi host
Execute the equivalent of the esxcli command “esxcli network diag ping” with the following paraemters:
Ping VC IP address
Only 3 time
Check the result and confirm that the ping is a success

Note: You should not use the “deprecated” command

PowerCLI providers

Connect to vCenter
List all “Windows PowerShell providers”
Identify the difference between the four defaults VMware PowerShell PSdrives
Copy VM logs of the first VM to a temp folder on the workstation using the VimDatastoreProvider
Disconnect from vCenter

Note:
The last task will be much more complicate if two datastores have the same name.
Do not check for it and consider that the environment is using proper naming convention avoiding duplicates datastore names.

PowerCLI configuration

Establish two VIServers connection. (It can be to two separate vCenter or multiples ESXi hosts)
Check the content of $global:DefaultVIServers
Configure PowerCLI to allow only one connection at a time to a VIServer for this PowerCLI session only using PowerCLI cmdlet.
Check the content of $global:DefaultVIServers
Establish again a connection to the first VIServer
Check the content of $global:DefaultVIServers

PowerShell function and strong parameter type

Create a PowerShell function that accept only one VMHost VIobject as a parameter and return the name of the ESXi host.
Test the function with one host
Same exercise with a distributed port group and this time return the name of the distributed port group.
Test the function with one distributed port group

List tag category with PowerCLI

Using only PowerCLI cmdlet
Create two new TAG category “TagCategoryTest” and “TagCategoryTest2”
List all TAG category using PowerCLI

List tag category with API and vCloud Suite

#Pre requisite vSphere 6
List all TAG category without using any of the following PowerCLI cmdlet
Get-Tag / Get-TagAssignment / Get-TagCategory

vCloud director

For this one you need to have access to vCloud director. There are not so many people who have access to this product since it is now only available for cloud provider.
Connect to cloud director
List all organization using PowerCLI cmdlet
Check the content of $global:DefaultCiServers
Explore .extensiondata of a default CIServer
List all organization using a method and not a PowerCLI cmdlet
Disconnect from cloud director

vRealize Operations Manager

For this one you need to have access to vROPS that is collecting performance metrics of an ESXi host.
Connect to vRops
Connect to the VC where is located the ESXi host
Extract for the first host the following performance metric CPU > Usage(%) for the last day with PowerCLI cmdlets.
Explore the content of $global:DefaultOMServers
Explore the extensiondata of the first OMServer
Obtain the license keys of the products
Disconnect from Vrops and vCenter

vCenter Site Recovery Manager

Connect to the vCenter server with VMs protected by SRM
Connect to SRM
Explore the content of $global:DefaultSrmServers
Explore the extensiondata of the first SrmServer
Get the name of all protected virtual machines
Disconnect SRM
Disconnect vCenter

vCloud Air Server

I didn’t have the opportunity to test vCloud Air yet, so there will be no answers for this exercise.
Connect to vCloud Air
Identify what you can do with PowerCLI cmdlets.
Identify if there is anything interesting in extensiondata of PIserver object.
Disconnect from vCloud Air

Conclusion

That’s it, you now know how to “think PowerCLI”
All the key concepts unique to PowerCLI have been presented.
You should also know now how to search in the documentation and use Onyx.
If you were already proficient in PowerShell, and if you know already VMware, this is all you need to handle any challenges.

What’s next?

Now you only need to practice to gain more experience.
I will recommend at the beginning to try to handle any new challenge in the same way as recommended for all exercises.
Try alone using official documentation, Onyx, and a test environment.
It will provide you the skills needed to create a script from scratch if needed.

After a while, you will reach a level of confidence where you will know that you can handle any challenge … as long as you have enough time in front of you.

This is where you will need to start to use existing scripts
It will be much faster to find, judge the quality, and adapt existing scripts than creating your own.
This is also where the “Receipe books” will be very useful, they will help you to kick start your own scripts.

There are also many areas that have not been covered in this training.
It is possible to nearly automatize all areas of VMware but not necessary with PowerCLI.
Connect to CIM of an ESXi server
Use REST to manage many products – Refers to recommend book
Automatize the installation of PowerCli or vCenter on Windows – Refers to recommend book
You can call from PowerShell putty to execute Linux command
etc

Regarding Rest you will have sometimes the options to pick between working with REST or via PowerCLI
This is the case for the vCloud Suite for example, you can use what has been described in this course or the vCloud Suite SDK for REST.
You will get more information about the others SDKS not necessary linked to PowerCLI in the Developer Center

If you would like to reach the guru level you can follow the advices in this guide:
PowerCLI study guide from rookie to guru

And to stay up to date always check if there is a new PowerCLI release available.

Appendix

Onyx

There are two versions of Onyx

Old Onyx:
Onyx
It works only with vSphere client, it means that you will not be able to capture settings that you can only configure from the vSphere Web Client.
Pro:
Easy to use and set up
No need to perfom any change at the vCenter level.
Con:
Only compatible with vSphere Client. Not possible to capture settings only available in WebClient.

New Onyx:
Onyx for the web client
Pro:
Compatible with Web Client and all settings only available with it
Con:
More complicated to setup
Change needed at the vCenter level

I have not tested yet the new Onyx due to the constraint at the vCenter level.
I will only recommend it in a test environment but it limits what you will be able to capture if your test environment doesn’t have all specifications of a production environment.
I use the following workaround with the old Onyx:
Execute the closest command to what I need.
Edit the output based on the official documentation to add all settings “unique” to vSphere 6.

Reference poster

VMware vSphere PowerCLI 6.3 Release 1 Reference Poster

Books recommended

Beginner:
Learning PowerCLI – February 2014
PowerCLI cookbook – March 2015

Advanced:
VMware vSphere PowerCLI Reference – January 2016
Mastering PowerCLI – October 2015
VMware VI and vSphere SDK: Managing the VMware Infrastructure and vSphere

I strongly recommend the VMware vSphere PowerCLI reference, even if the title is misleading.
It describes how to automate many areas of VMware but not only with PowerCLI.

Read all books.
They will often contain similar information but it is a good way to learn different way to achieve the same result.

Blogs recommended

VMware PowerCLI Blog
LucD notes
virtuallyGhetto
virtu-al.net

The list will probably evolves in the future I just end up very often on these blogs while looking for information.

One thought on “PowerCLI study guide – core concepts

  1. Pingback: PowerCLI study guide – core concepts | Yogesh

Leave a Reply

Your email address will not be published. Required fields are marked *