VCAP5-DCA Objective 8.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI

Knowledge

  • Identify vSphere PowerCLI requirements
  • Identify Cmdlet concepts
  • Identify environment variables usage

Skills and Abilities 

  • Install and configure vSphere PowerCLI
  • Install and configure Update Manager PowerShell Library
  • Use basic and advanced Cmdlets to manage VMs and ESXi Hosts
  • Use Web Service Access Cmdlets
  • Use Datastore and Inventory Providers
  • Given a sample script, modify the script to perform a given action

 

Install and configure vSphere PowerCLI

Official Documentation:

vSphere Power CLI User’s Guide 5.0, Chapter 3 “Installing vSphere PowerCLI”, page 15.

PowerCLI Installation Prerequisites
To install vSphere PowerCLI, you must have installed the following software: 

  • .NET 2.0 SP1
  • Windows PowerShell 1.0/2.0

Supported Operating Systems

VMware vSphere PowerCLI 5.0 is supported on the 32-bit and 64-bit versions of the following Windows operating systems:

  • Windows 7
  • Windows Server 2008
  • Windows Vista
  • Windows XP Service Pack 2
  • Windows 2003 Server Service Pack 2

Supported VMware Environments

vSphere PowerCLI 5.0 is compatible with the following VMware environments:

  • VMware ESXi 5.0
  • vCenter Server 5.0
  • VMware ESX 4.1/vCenter Server 4.1
  • VMware ESXi 4.1
  • VMware ESX 4.0 Update 2/vCenter Server 4.0 Update 2
  • VMware ESX 4.0 Update 1/vCenter Server 4.0 Update 1
  • VMware ESX 4.0i Update 1
  • VMware ESX 3.5 Update 5
  • VMware ESXi 3.5 Update 5
  • VMware VirtualCenter 2.5 Update 6
  • VMware ESX 3.0.3 Update 1

Downloading the vSphere PowerCLI Binary

You can download the latest vSphere PowerCLI version from the vSphere PowerCLI home page at http://www.vmware.com/go/powercli.
Installing vSphere PowerCLI on Windows

To install vSphere PowerCLI using the Windows installer

  1. Launch the installer by double-clicking the executable file you downloaded. See “Downloading the vSphere PowerCLI Binary” on page 13.
  2. Click Next in the Welcome page to continue with the installation.
  3. Read and accept the license agreement terms.
  4. Click Next.
    The Destination Folder page appears.
  5. If you do not want to install the vSphere PowerCLI in the default location, click Change and select a different directory.
    The default location is:
    C:\Program Files\VMware\Infrastructure\vSphere PowerCLI
  6. Click Next.
  7. In the Ready to Install the Program page choose whether to create a desktop shortcut.
  8. Click Install to proceed with the installation.
  9. Click Finish to complete the installation process.

If the PowerShell Execution Policy on your machine is set incorrectly, a warning message appears before finalizing the vSphere PowerCLI installation. Ignore it and continue with the installation.
Setting the Properties to Support Remote Signing

For security reasons, Windows PowerShell supports an execution policy feature. It determines whether scripts are allowed to run and whether they must be digitally signed. By default, the execution policy is set to Restricted, which is the most secure policy. If you want to run scripts or load configuration files, you can change the execution policy by using the Set-ExecutionPolicy cmdlet.
To set the PowerShell execution policy

  1. Choose Start > Programs > VMware > VMware vSphere PowerCLI> VMware vSphere PowerCLI. The vSphere PowerCLI console window opens.
  2. In the vSphere PowerCLI console window, type:
    Set-ExecutionPolicy RemoteSigned
    To get more information about the execution policy and script digital signing in Windows PowerShell, use the following command:

    Get-Help About_Signing

 

Install and configure Update Manager PowerShell Library

Official Documentation:

VMware vSphere Update Manager PowerCLI Installation and Administration Guide

The VMware vSphere Update Manager PowerCLI provides a set of cmdlets for downloading software patches, creating and modifying baselines, and for scanning and remediating virtual machines or hosts. These cmdlets are stored in the VMware.VumAutomation plug-in, and are available through the VMware vSphere PowerCLI console.
Installing Update Manager PowerCLI

You can install and use Update Manager PowerCLI on machines that have VMware vSphere PowerCLI installed and have access to a vCenter Server system. You can install Update Manager PowerCLI on a machine that does not have Update Manager or vCenter Server installed.
Supported Platforms

Update Manager PowerCLI 5.0 is supported on the 32-bit and 64-bit versions of the following Windows operating systems:

  • Windows 7
  • Windows Server 2008
  • Windows Vista
  • Windows XP Service Pack 2
  • Windows Server 2003 Service Pack 2

Prerequisites

To install and use Update Manager PowerCLI 5.0, you must have the following software installed on the target machine:

  • .NET 2.0 SP1
  • Windows PowerShell 2.0
  • VMware vSphere PowerCLI 4.1 or higher

Install Update Manager PowerCLI

You can download the Update Manager PowerCLI installer package from the product landing page at https://www.vmware.com/support/developer/ps-libs/vumps/.

To install the Update Manager PowerCLI

  1. Start the Update Manager PowerCLI installer.
  2. Click Next in the Welcome page to continue with the installation.
  3. Read and accept the license agreement terms.
  4. Click Install.
  5. Click Finish to complete the installation process.

Uninstalling Update Manager PowerCLI

To uninstall the Update Manager PowerCLI from your Windows system, you can use the Add or Remove Programs utility.

 

Use basic and advanced Cmdlets to manage VMs and ESXi Hosts

Official Documentation:

vSphere Power CLI User’s Guide 5.0, Chapter 4 “vSphere PowerCLI Usage Examples”, page 17.
Display Help for a Cmdlet

You can get help for a specific cmdlet by supplying the Get-Help command in the vSphere PowerCLI console.

To get information on a cmdlet

  1. Run the Get-Help cmdlet with the specific cmdlet name:
    Get-Help Add-VMHost
  2. (opt) Retrieve more detailed information by using the -full parameter:
    Get-Help Add-VMHost -full

Connect to a Server

In vSphere PowerCLI, you can have more than one connections to the same server. To disconnect from a server, you must close all active connections to this server running the Disconnect-VIServer cmdlet.

To connect to a local server

  1. Run the Connect-VIServer cmdlet with the server name:
    Connect-VIServer -Server esx3.example.com

The cmdlet prompts for user credentials, as they are not passed as parameters.
To manage virtual machines

  1. After establishing a connection to a server, list all virtual machines on the target system:
    Get-VM
  2. Save the name and the power state properties of the virtual machines in the ResourcePool resource pool into a file named myVMProperties.txt:
    $respool = Get-ResourcePool ResourcePool
    Get-VM -Location $respool | Select-Object Name, PowerState > myVMProperties.txt

  3. Start the VM virtual machine:
    Get-VM VM | Start-VM
  4. Retrieve information of the guest OS of the VM virtual machine:
    Get-VMGuest VM | fc
  5. Shutdown the OS of the VM virtual machine:
    Shutdown-VMGuest VM
  6. Power off the VM virtual machine:
    Stop-VM VM
  7. Move the virtual machine VM from the Host01 host to the Host02 host:
    Get-VM -Name VM -Location Host01 | Move-VM –Destination Host02

For more information about PowerCLI command lines see the following resources:

 

Use Web Service Access Cmdlets

Official Documentation:

vSphere Power CLI User’s Guide 5.0, Chapter 4 “vSphere PowerCLI Usage Examples”, section “API Access Cmdlets”, page 33.
The vSphere PowerCLI list of cmdlets includes two API Access cmdlets:

  • Get-View
  • Get-VIObjectByVIView

They enable access to the programming model of the vSphere SDK for .NET from PowerShell and can be used to initiate vSphere .NET objects
The documentation presents some usage examples.
More information:

 

Use Datastore and Inventory Providers

Official Documentation:

vSphere Power CLI User’s Guide 5.0, Chapter 4 “vSphere PowerCLI Usage Examples”, section “The Inventory Provider” and “The Datastore Provider”, page 35.
The Inventory Provider

The Inventory Provider (VimInventory) is designed to expose a raw inventory view of the inventory items from a server. It enables interactive navigation and file-style management of the VMware vSphere inventory.

By creating a PowerShell drive based on a managed object (such as a datacenter), you obtain a view of its contents and the relationships between the items. In addition, you are able to manipulate objects (move, rename or delete them) by running commands from the vSphere PowerCLI console.

When you connect to a server with Connect-VIServer, the cmdlet builds two default inventory drives: vi and vis. The vi inventory drive shows the inventory on the last connected server. The vis drive contains the inventory all vSphere servers connected within the current vSphere PowerCLI session. 

You can use the default inventory drives or create custom drives based on the default ones.
Basic Functions of the Inventory Provider

The following procedure illustrates some basic operations with the inventory provider.

To view the content of a default inventory drive

  1. Access the vi inventory drive:
    cd vi:
  2. List the drive content:
    dir
    dir is an alias of the Get-ChildItem cmdlet.

To create a new custom inventory drive

  1. Get the root folder of the server:
    $root = Get-Folder -NoRecursion
  2. Create a PowerShell drive named myVi in the server root folder:
    New-PSDrive -Location $root -Name myVi -PSProvider VimInventory -Root ‘\’

A different way to create a inventory drive is to map an existing inventory path:

New-PSDrive -Name myVi -PSProvider VimInventory -Root “vi:\Folder01\Datacenter01”
To manage inventory objects through inventory drives

  1. Navigate through your server inventory by running the cd command with the full path to the host:
    cd Folder01\DataCenter01\host\Web\Host01
  2. List the content of the host using the ls command:
    ls
    ls is the UNIX style alias of the Get-ChildItem cmdlet.
    This command returns the virtual machines and the root resource pool of the host.
  3. View only the virtual machines on the host:
    Get-VM
    When called within the inventory drive, Get-VM retrieves only the virtual machines on the current drive location.
  4. Delete a virtual machine named VM1:
    del VM1
  5. Rename a virtual machine from VM1New to VM1:
    ren VM1New VM1
  6. Start all virtual machines whose names start with VM:
    dir VM* | Start-VM

The Datastore Provider

The Datastore Provider (VimDatastore) is designed to provide access to the contents of one or more datastores. The items in a datastore are files that contain configuration, virtual disk, and the other data associated with a virtual machine.All file operations are case-sensitive. 

When you connect to a server with Connect-VIServer, the cmdlet builds two default datastore drives:

vmstores and vmstore. The vmstore drive displays the datastores available on the last connected vSphere server. The vmstores drive contains all datastores available on all vSphere servers connected within the current vSphere PowerCLI session.

You can use the default inventory drives or create custom drives based on the default ones.
Basic functions of the Datastore Provider

The following procedures illustrate some basic functions of the Datastore Provider.

To browse default datastore drives

  1. Access the vmstore drive:
    cd vmstore:
  2. Access the vmstores drives:
    cd vmstores:
  3. List the drive content:
    dir

To create a new custom datastore drive

  1. Get a datstore by its name and assign it to the $datastore variable:
    $datastore = Get-Datastore Storage1
  2. Create a new PowerShell drive ds: in $datastore:
    New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root ‘\’

A different way to create a datastore drive is to map an existing datastore path. For example:

New-PSDrive -Name ds -PSProvider VimDatastore -Root

vmstore:\Folder01\Datacenter01\Datastore01\Folder01

or

New-PSDrive -Name ds -PSProvider VimDatastore -Root

vmstores:\192.168.10.112@443\Folder01\Datacenter01\Datastore01\Folder01

To manage datastores through datastore drives

  1. Navigate to a specific folder on the ds: drive:
    cd VirtualMachines\XPVirtualMachine
  2. List the files of the folder, using the ls command:
    ls
    ls is the UNIX style alias of the Get-ChildItem cmdlet.
  3. Rename a file, using the Rename-Item cmdlet or its alias ren. For example, to change the name of the
    vmware-3.log file to vmware-3old.log, run the following command:
    ren vmware-3.log vmware-3old.log
    All file operations apply only on files in the current folder.
  4. Delete a file, using the Remove-Item cmdlet or its alias del. For example, to remove the vmware-3old.log file from the XPVirtualMachine folder, use the following command:
    del ds:\VirtualMachines\XPVirtualMachine\vmware-2.log
  5. Copy a file, using the Copy-Item cmdlet or its alias copy:
    copy ds:\VirtualMachines\XPVirtualMachine\vmware-3old.log ds:\VirtualMachines\vmware-3.log
  6. Copy a file to another datastore, using the Copy-Item cmdlet or its alias copy:
    copy ds:\Datacenter01\Datastore01\XPVirtualMachine\vmware-1.log
    ds:\Datacenter01\Datastore02\XPVirtualMachine02\vmware.log
  7. Create a new folder, using the New-Item cmdlet or its alias mkdir:
    mkdir -Path ds:\VirtualMachines -Name Folder01 -Type Folder
  8. Download a file to the local machine using the Copy-DatastoreItem cmdlet:
    Copy-DatastoreItem ds:\VirtualMachines\XPVirtualMachine\vmware-3.log C:\Temp\vmware-3.log
  9. Upload a file from the local machine, using the Copy-DatastoreItem cmdlet:
    Copy-DatastoreItem C:\Temp\vmware-3.log ds:\VirtualMachines\XPVirtualMachine\vmware-3new.log

 

Other exam notes

 

VMware vSphere official documentation

VMware vSphere Basics Guide html pdf epub mobi
vSphere Installation and Setup Guide html pdf epub mobi
vSphere Upgrade Guide html pdf epub mobi
vCenter Server and Host Management Guide html pdf epub mobi
vSphere Virtual Machine Administration Guide html pdf epub mobi
vSphere Host Profiles Guide html pdf epub mobi
vSphere Networking Guide html pdf epub mobi
vSphere Storage Guide html pdf epub mobi
vSphere Security Guide html pdf epub mobi
vSphere Resource Management Guide html pdf epub mobi
vSphere Availability Guide html pdf epub mobi
vSphere Monitoring and Performance Guide html pdf epub mobi
vSphere Troubleshooting html pdf epub mobi
VMware vSphere Examples and Scenarios Guide html pdf epub mobi

Related articles:

Disclaimer.
The information in this article is provided “AS IS” with no warranties, and confers no rights. This article does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.

Marco

Marco works for ViaData as a Senior Technical Consultant. He has over 15 years experience as a system engineer and consultant, specialized in virtualization. VMware VCP4, VCP5-DC & VCP5-DT. VMware vExpert 2013, 2014,2015 & 2016. Microsoft MCSE & MCITP Enterprise Administrator. Veeam VMSP, VMTSP & VMCE.