[VMUG Austria] PowerCLI 101

[VMUG Austria] PowerCLI 101

This is the second part of the guide line of my VMUG session of PowerCLI 101. In first part I focused on PowerShell basics.

PowerCLI Installation

Since version 6.5.1.5 of PowerCLI it is just available on Microsoft PowerShell Gallery. Until than it was a separate download on VMware pages. The advantage of PowerShell Gallery is that modules can be installed by using PowerShell command:

install-module

To use this you need an current version of PowerShell like 5.1. You can show your version by running

(get-host).version

To list already installed VMware modules on your PowerShell host, run:

get-module vmware* -ListAvailable

If your version of PowerCLI is older than 6.5.1.5 you have to uninstall it before you can install a version from PowerShell Gallery. To query versions in PowerShell Gallery run:

find-module vmware.powercli
find-module vmware.powercli -AllVersions | select name, version, publisheddate

First command shows only latest version. Second shows all available version.

To install PowerCLI use these command:

install-module vmware.powercli -allowClobber

Option “allowClobber” allows installing modules including cmdlets with names that are already installed on the host. If your host does not have access to internet, install PowerCLI on a host that has. Use this command to export module:

Save-Module -Name VMware.PowerCLI -Path C:\PathToDestination\

To update PowerCLI to current version – when installed version already from PowerShell Gallery:

Update-Module vmware.powercli

After PowerShell is installed, it has to be imported in current session to use it. This is done with the command:

import-module vmware.powercli

Since latest versions and Windows 10 and Server 2016 it is necessary to configure certificate handling in PowerCLI. Otherwise it is not possible to connect to vCenter or host using untrusted certificates.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session

Get help

There are build-in cmdlets in PowerShell to get help. The following I use more often.

Get-Command
Get-Command -Module VMware.VimAutomation.Core
Get-Command -Module VMware.VimAutomation.Core get-*

Cmdlets:

1: list all available cmdlets – if module is installed or not
2: list all cmdlets of a specific module – in this example VMware.VimAutomation.Core
3: list all cmdlets, beginning with “get-” of a specific module.

To get more information of a cmdlet, use:

Get-Help
Get-Help Get-VM
Get-Help Get-VM -Examples

1: Shows help for get-help
2: Shows information of cmdlet Get-VM
3: Shows examples for cmdlet Get-VM

Object Cmdlets

There are a few Cmdlets that can just be used to pipe something in. Three of them are very common.

Where-Object

With this cmdlet, it is possible to filter within the result of a cmdlet. This is useful if cmdlet itself does not offer the option to filter. This is to prefer.

Get-VM
Get-VM | Where-Object {$_.MemoryGB -gt 8}

1: Show all VMs
2: Show all VMs with more than 8GB configured memory.
You can see in this example that variable $_ is used to refer to objects came from pipe.

Select-Object

When running a PowerCLI cmdlet a pre-defined set of columns are shown. This does not mean there isn’t more to see. Using Select-Object, you can select columns to show. A command I use very often:

Get-VMHost | Select-Object Name, Version, Build

Sort-Object

It is easy to imagine what this cmdlet does. Here two examples:

Get-VM | Sort-Object Name
Get-VM | Sort-Object Name -Descending

1: Show all VMs in ascending order which is the default order.
2: Show all VMs in descending order.

PowerCLI Usage

To use PowerCLI you need to connect to a vCenter instance or a single host. To do this use this command:

Connect-VIServer vCenter

To show how to use PowerCLI, I show how to mange VM port groups on standard vSwitches. This is something time-consuming and error-prone. Automating this tasks by using PowerCLI is a way to overcome this – without the need for any additional licences.

At first we take a look at the vSwitch configuration of a host managed by vCenter:

Get-VMHost
Get-VMHost hostname | Get-VirtualSwitch
Get-VMHost hostname | Get-VirtualSwitch -name vSwitch1 | Get-VirtualPortGroup

1: Shows all ESXi hosts, managed by vCenter.
2: Shows all vSwitches of host hostname.
3: Shows all port groups of vSwitch1 of host hostname.

Now we add a new port group to this vSwitch. In our example we call it “DMZ” and use VLAN ID 999. As you can see, cmdlet New-VirtualPortGroup gets vSwitch to create portgroup as input from pipe:

Get-VMHost hostname | Get-VirtualSwitch -name vSwitch1 | New-VirtualPortGroup -Name "DMZ" -VLanId 999

To change VLAN ID – to 777 in this example – of an existing port group, run:

Get-VMHost hostname | Get-VirtualSwitch -name vSwitch1 | Get-VirtualPortGroup -Name "DMZ" | Set-VirtualPortGroup -VLanId 777

To remove a port group, tun:

Get-VMHost hostname | Get-VirtualSwitch -name vSwitch1 | Get-VirtualPortGroup -name "DMZ"| Remove-VirtualPortGroup -Confirm:$false

Use option “-Confirm:$false” to avoid PowerShell to ask if you are sure to do this.

This examples should illustrate how to use PowerCLI. We did all task for a specific host. But how to do this for, lets say all hosts of a cluster? Exactly here we can see the power of PowerShell respectively PowerCLI. We just need to select the hosts we want to perform theses task for and pipe them to the rest of our cmdlets. For example, we want to do this VLAN management for all hosts in cluster “Main-Cluster”. These commands can be used:

Get-Cluster "Main-Cluster" | Get-VMHost | Get-VirtualSwitch -name vSwitch1 | New-VirtualPortGroup -Name "delete" -VLanId 999
Get-Cluster "Main-Cluster" | Get-VMHost | Get-VirtualSwitch -name vSwitch1 | Get-VirtualPortGroup -Name delete | Set-VirtualPortGroup -VLanId 777
Get-Cluster "Main-Cluster" | Get-VMHost | Get-VirtualSwitch -name vSwitch1 | Get-VirtualPortGroup -name delete | Remove-VirtualPortGroup -Confirm:$false

If tasks have to be done for alle hosts, managed by vCenter, start command line with Get-VMHost.

Tips and Tricks

A way to check if a download is error-free, checksums are very useful. With PowerShell, you can easily calculate them for files. For example MD5-checksum:

Get-FileHash -Algorithm MD5 file.txt

When you generate an important list and want to send it per mail you can export the list to an HTML file. For example:

Get-VM | ConvertTo-Html | Out-File -FilePath c:\temp\out.html

Often the default output list cuts off width of columns. To overcome this, try Format-Table with option AutoSize

Get-VMHost | Format-Table -AutoSize

When you are familiar with shell in ESXi you probably know esxcli. This command does not know tab-completion or shortening of input. With PowerCLI you get tab-completion! To get information, see this example:

$esxcli = Get-VMHost kbcesx01.kbc.lab | Get-EsxCli
$esxcli.  # try tab
$esxcli.system.hostname.get()

1: Sets $esxcli for a host
2: Try tab-completion
3: complete command

To see all information a cmdlet can show (in first instance) you can use Select-Object:

Get-VMHost
Get-VMHost | Select-Object *

1: Show pre-defined data set of  Get-VMHost.
2: Show all possible data-columns. Using “.” you can dive deeper…

Links

Leave a Reply

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