Documente Academic
Documente Profesional
Documente Cultură
Table of Contents
Lab Overview - HOL-SDC-1607 - From Beginner to Advanced Features with PowerCLI ..... 2
Lab Guidance .......................................................................................................... 3
Module 1: From Beginner to Advanced Features with PowerCLI - (60 Minutes)................. 4
Module overview ..................................................................................................... 5
Getting Started With PowerShell and PowerCLI....................................................... 6
Using PowerCLI for reporting ................................................................................ 11
Exporting report results to various formats........................................................... 14
Setting up and configuring a cluster ..................................................................... 20
Moving hosts to the cluster ................................................................................... 24
Setting up and configuring virtual distributed switch............................................ 25
Creating a VM and vMotioning it between the hosts ............................................. 28
Creating multiple VMs and tagging them appropriately........................................ 30
Modifying multiple VMs based on their tag ........................................................... 34
Configuring and deploying an OFV template ........................................................ 35
Further Reading..................................................................................................... 37
HOL-SDC-1607 Page 1
HOL-SDC-1607
HOL-SDC-1607 Page 2
HOL-SDC-1607
Lab Guidance
This lab covers PowerCLI for the beginning and advanced users.
NOTE: If you are using a device with non-US keyboard layout, you might find it
difficult to enter CLI commands, user names and passwords throughout the
modules in this lab.
We have included videos throughout the modules of this lab. To get the most out of
these videos, it is recommenced that you have headphones to hear the audio. The
timing of each video is noted
Lab Captains: Phil Balfanz, Paval Dimitrov, Pierre Grothe, Tom Lusk, Carl Olafson
This lab manual can be downloaded from the Hands-on Labs Document site found here:
http://docs.hol.pub/HOL-2016/hol-sdc-1607_pdf_en.pdf
HOL-SDC-1607 Page 3
HOL-SDC-1607
HOL-SDC-1607 Page 4
HOL-SDC-1607
Module overview
This module will introduce you to VMware vSphere PowerCLI. Novice users will easily
learn to use the tool and more advanced users will get familiar with the new
functionality available in the latest releases of the product like configuring an OVA
before deploying it and filtering objects by their tags. Both new and experienced users
are sure to learn something new.
HOL-SDC-1607 Page 5
HOL-SDC-1607
Starting PowerCLI
After installation, PowerCLI can be started by double clicking the desktop icon labeled
"VMware vSphere PowerCLI". This will open the PowerShell console and load all PowerCLI
modules.
HOL-SDC-1607 Page 6
HOL-SDC-1607
As an alternative to the simple console, you can use Powershell ISE script editor, which
provides a better user experience. You can start the editor by double clicking its icon on
the desktop (1). The upper pane (2) is for viewing/editing script files, and the lower pane
(3) is for running individual commands and displaying their output (an analog of the
standard PowerShell console).
HOL-SDC-1607 Page 7
HOL-SDC-1607
During this lab you will execute various PowerCLI commands in the lower pane of
PowerShell ISE or in the PowerShell console. You can either type the commands yourself
or copy-paste them from the "Module7.ps1" files located on the desktop. The most
convenient way to go through the lab is to open Module7.ps1 in the upper pane of
Powershell ISE. This file contains all commands you will call in this module. Each
command can be selected (1) and executed by pressing F8 (or "Run Selection" button
(2)). Before executing a new command make sure the previous one is completed - you
will see "Completed" message (3) at the bottom of the console.
Let's explore what we can do with PowerCLI. PowerCLI's snapins provide more than 500
commands (called cmdlets in PowerShell) for managing vSphere, vCloud Air, SRM, vR
Ops, and VUM. You can view the available PowerCLI commands by typing:
Get-VICommand
HOL-SDC-1607 Page 8
HOL-SDC-1607
This will list all PowerCLI commands. As the list is quite large, you may want to narrow it
down to something more specific, for example all commands for managing VMs:
Get-VICommand *VM
Hint: You can use autocomplete for faster typing - just start typing the beginning of the
command/parameter and press "Tab".
Connecting to vSphere
The first thing we need to do in order to manage our vCenter Server is to connect to it.
This is done by using the Connect-VIServer command. Our vCenter is named "vcsa-01a"
and here's how to connect to it:
The command will connect to the vCenter with the specified user's credentials.
Getting Help
If you are unsure how to use a specific cmdlet, you can easily view its help by typing
Get-Help (or for short - just "help") and the name of the cmdlet:
help Connect-VIServer
If you want to see the full help with example usages of the cmdlet and parameter
descriptions you should open the full help of the cmdlet:
If you want to see only the examples, you can use -examples switch like this:
You can also search for a specific word in the entire help archive. Let's try searching for
a cmdlet that vmotions VMs:
HOL-SDC-1607 Page 9
HOL-SDC-1607
help vmotion
The result contains the cmdlet we need - Move-VM. We'll use it later in this module.
Feel free to check the help of any cmdlet we demonstrate in this lab.
HOL-SDC-1607 Page 10
HOL-SDC-1607
Retrieving VMs
One of the most common things to do is list the VMs in your vCenter Server. This is
useful for browsing the inventory and reporting as well as for further processing of
specific VMs (e.g. batch modification of VMs). You can retrieve all VMs with:
Get-VM
You can also retrieve one or more VMs by name. Try out the following:
Get-VM linux-CPU-Load-01a
Get-VM linux*
HOL-SDC-1607 Page 11
HOL-SDC-1607
When you call a cmdlet (like Get-VM) the result you get is an object or array of objects
(in this case it's a VM object). To see what properties the object(s) have you can use
Get-Member cmdlet like this:
Note: Here we use a PowerShell functionality called "pipeline" (or "piping"). Get-Member
cmdlet actually has a parameter called "InputObject" that accepts VM objects (as well
as all other PowerShell objects). Instead of retrieving our VMs, storing them in a variable
and passing them to that parameter of Get-Member, we simply "pipe" the output of Get-
VM to Get-Member. This way the output of the first cmdlet becomes the input of the
second.
Then you can use PowerShell's 'select' command to retrieve only the properties you
need from resulting object:
HOL-SDC-1607 Page 12
HOL-SDC-1607
When the Get-* cmdlet has no parameter to filter the objects by the property you need,
you can use PowerShell's 'where' command to filter the results from a cmdlet call:
Hint: $_ is a reserved powershell variable that holds the current object from the pipeline
HOL-SDC-1607 Page 13
HOL-SDC-1607
HOL-SDC-1607 Page 14
HOL-SDC-1607
Exporting to txt
Let's start with the most basic export - to txt file. To export your report to txt use Out-
File PowerShell cmdlet:
HOL-SDC-1607 Page 15
HOL-SDC-1607
Exporting to csv
Now let's export the same data to csv format. We'll use Export-Csv cmdlet to do that:
HOL-SDC-1607 Page 16
HOL-SDC-1607
Exporting to xml
The next format that you'll export to is XML. The PowerShell cmdlet we'll use is
ConvertTo-Xml. Since this cmdlet returns XMLDocument object we need to call its Save
method to write formatted data to file:
HOL-SDC-1607 Page 17
HOL-SDC-1607
Exporting to HTML
You can also export the data in HTML format by using ConvertTo-Html cmdlet. Since this
command just formats the data in HTML you also need Set-Content cmdlet to write
formatted data to file:
HOL-SDC-1607 Page 18
HOL-SDC-1607
If we want to make our HTML report look a bit better we can apply some styles on it like
this:
$a = "<style>"
$a = $a + "BODY{background-color:Gainsboro;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;bord
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color:
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color:
$a = $a + "</style>"
Get-VM | Select Name, NumCPU, MemoryMB, PowerState, VMHost | ConvertTo-Html -he
If you open the report now you'll see that it looks much better
HOL-SDC-1607 Page 19
HOL-SDC-1607
Creating a cluster
To create a new cluster we'll use the New-Cluster cmdlet. We have to specify the name
and the location of the new cluster:
HOL-SDC-1607 Page 20
HOL-SDC-1607
Now let's configure the cluster's enhanced vMotion compatibility mode to ensure CPU
compatibility for vMotion. To do that we'll first determine what EVC modes are supported
by our hosts:
Now inspect the content of the $evcModes variable. Just type the name of the variable
($evcModes) and its value will be printed to the screen. You'll notice that both hosts
have the same evc mode.
Note: The EVC mode might be different from the one on the screenshot above, because
it depends on the physical hardware that your lab environment runs on.
Let's set that EVC mode to the cluster to make sure that any other hosts that might be
added to the cluster in the future will have to match this EVC mode:
Hint: Prompting for confirmation is good for interactive usage, however it is undesired in
scripts since it will halt them. You can automatically confirm the operation by appending
"-Confirm:$false" parameter to your cmdlet call.
HOL-SDC-1607 Page 21
HOL-SDC-1607
The next step is to enable HA on our cluster to ensure that our VMs are protected in
case of host failure:
Now let's enable DRS on this cluster and set its automation level to "manual" to make it
generate recommendations for VM placement and migration (you can retrieve and apply
HOL-SDC-1607 Page 22
HOL-SDC-1607
HOL-SDC-1607 Page 23
HOL-SDC-1607
To move the hosts into the new cluster we need to put them in maintenance mode first.
To put them in maintenance mode we have to shut down all the running VMs before
that. So, let's retrieve all the running VMs and then stop them:
Next we'll use the Move-Host cmdlet to move the hosts to the cluster:
And at the end let's take the hosts out of maintenance mode:
HOL-SDC-1607 Page 24
HOL-SDC-1607
Next step is to add both our ESX hosts to the distributed switch:
You can verify ESX hosts are successfully added in the distributed switch:
Now let's create portgroups for the management, storage, vMotion and VM networks
with the New-VDPortGroup cmdlets:
HOL-SDC-1607 Page 25
HOL-SDC-1607
We'll migrate our hosts' networking host by host. First we'll save both our hosts in a
variable:
Then we'll migrate the networking for each one of them by using PowerShell's foreach
loop. For each host we'll first retrieve its physical nics and then its management,
storage, and vMotion VMKernel nics. Then we'll migrate them together by using the Add-
VDSwitchPhysicalNetworkAdapter cmdlet, while specifying the distributed portgroups for
each VMKernel nic with the VirtualNicPortGroup parameter:
HOL-SDC-1607 Page 26
HOL-SDC-1607
In the end let's migrate the VM networking as well. First we'll get the network adapters
of all the VMs:
Next we'll migrate these network adapters to the "VM" portgroup that we created in the
previous step:
Now the network traffic of all the VMs is going through the VDSwitch.
HOL-SDC-1607 Page 27
HOL-SDC-1607
Let's start by creating a VM with the New-VM cmdlet. We'll specify the VM's name, hard
disk and memory size, the host and the datastore, on which we want to create it. Also
we'll specify the portgroup for the VM network to be in the distributed switch, created
earlier. We'll place the VM on our NFS datastore, which is shared between our two hosts.
$vm | Start-VM
Now let's move the VM to the other host to make the workload more equally distributed:
Note: If this is not the first module you're doing in this session, there is a chance that
you may have some disconnected datastores in your environment. In this case you'll
receive an error that Move-VM is unable to access the virtual machine configuration. If
this happens you'll have to reconnect the NFS datastore on one of your hosts. To do so
we'll use Get-EsxCli cmdlet:
$esxCli.storage.nfs.list()
HOL-SDC-1607 Page 28
HOL-SDC-1607
...
Accessible : false
...
$esxCli.storage.nfs.remove('ds-site-a-nfs01')
Tip: Since Move-VM operation could take significantly long time, you may want to move
the VMs asynchronously and continue your work in the meantime. You can do that by
specifying -RunAsync parameter of Move-VM. Many other cmdlets that may take long
time to complete also support -RunAsync.
HOL-SDC-1607 Page 29
HOL-SDC-1607
First of all let's review the specification that we'll follow when creating the VMs. Open
the 'vmspec.csv' file located on your desktop. For every VM we have the name, memory
size (in MB), disk size (in GB), department and type (user or server VM).
Inspect the content of the $spec variable. You'll find out that it contains an array of
objects (one object for each VM) and each one of these objects has a corresponding
property for name, memory size, disk size, department and type.
HOL-SDC-1607 Page 30
HOL-SDC-1607
Now let's create the VMs based on the specification. We'll use the foreach PowerShell
loop again:
Our next task is to tag our newly created VMs appropriately. To do that we'll start by
creating two different tag categories - "Department" and "Type":
HOL-SDC-1607 Page 31
HOL-SDC-1607
Get-Tag
HOL-SDC-1607 Page 32
HOL-SDC-1607
Now when we have the tags and tag categories properly defined it's time to tag the VMs
appropriately. We'll use the New-TagAssignment to assign a tag to a VM:
HOL-SDC-1607 Page 33
HOL-SDC-1607
A new application will be deployed in the Nephosoft sales department and that requires
the memory on the user machines to be updated to 1GB. Now that we have our VMs
properly tagged it's easier to make a mass update of the VMs. Let's retrieve all the VMs
from the sales department first:
Note: We cannot use "Get-VM -Tag sales, user", because that will return all the VMs are
either from the sales department, or user VMs and what we actually want is to get the
VMs that are both from the sales department and are user VMs
Now let's select all VMs that have both tags assigned. For that purpose we'll use
Compare-Object Powershell cmdlet:
Before we can update the VMs memory we need to make sure that they are all stopped:
Finally we'll update the VM's memory capacity with the Set-VM cmdlet:
HOL-SDC-1607 Page 34
HOL-SDC-1607
Take a look at the object inside the $linuxMicroConfig variable. It contains one property,
which points to the source ovf file and second, which is the actual configurable property
for the selected ovf file - NetworkMapping. When we take a look inside the
NetworkMapping object we'll see that it contains single property VM_Network, which is
the only configuration that can be applied to that ovf tempalte.
You can update the retrieved configuration just by assigning values to the object
properties, matching the configurations you want to apply. In our case we will assign
value to the "VM_Network"
$linuxMicroConfig.NetworkMapping.VM_Network.Value = 'VM'
When you import your vApp you can apply its configuration using the new Import-VApp
parameter "OvfConfiguration".
HOL-SDC-1607 Page 35
HOL-SDC-1607
As you can see, the vApp has been deployed with the correct network mapping.
HOL-SDC-1607 Page 36
HOL-SDC-1607
Further Reading
This lesson is for information purpose only and lists some of the more advanced features
of PowerCLI, useful reading materials for both novice and advanced users as well as
information for other PowerCLI related labs. Feel free to experiment with these if you
like.
HOL-HBD-1683 Managing Your Hybrid Cloud: Module 3 is all about managing vCloud Air
with VMware vSphere PowerCLI.
Although PowerCLI offers more than 300 cmdlets for managing vSphere they don't cover
the entire functionality of the platform. Despite that you are still able to access all of the
functionality by using the Get-View cmdlet. This special cmdlet is an access point to the
entire vSphere API. The objects returned by the cmdlet are known as "Views" and
represent an exact copy of the vSphere API objects. You can find more information about
these objects in the vSphere API Reference
Documentation:
http://pubs.vmware.com/vsphere-60/index.jsp
Using this cmdlet you will be able to write scripts directly against the API in an object-
oriented manner.
Onyx for the Web Client is a Fling that translates actions taken in the vSphere Web
Client to PowerCLI.Net code. This Fling was the winning entry in last years 2014 Fling
Contest. Its an update, of sorts, to the Onyx fling, as it allows for similar functionality in
the web client.
The Onyx for the Web Client Fling provides the ability to record actions taken in the
vSphere Web Client and turn these actions into PowerCLI.Net code. The resulting code
can then be used to understand how VMware performs an action in the API and also
better define functions. You could also input the resulting code into search engines to
find the API documentation and information on how to use this area of the vSphere API.
HOL-SDC-1607 Page 37
HOL-SDC-1607
https://labs.vmware.com/flings/onyx-for-the-web-client
If you are new to PowerCLI or want to learn more about the product then here are a few
blogs you can follow. There you can find great articles and scripts about PowerCLI:
HOL-SDC-1607 Page 38
HOL-SDC-1607
Conclusion
Thank you for participating in the VMware Hands-on Labs. Be sure to visit
http://hol.vmware.com/ to continue your lab experience online.
Version: 20160223-080502
HOL-SDC-1607 Page 39