Skip to content

Getting Started with PowerCLI 5.1 and PowerShell 3.0

In this post i am going to give a quick startup guide to the new version of PowerCLI 5.1 with the Windows Management Framework 3.0, which is available with Windows 8. Both are recently published releases of the great PowerShell scripting engine that every VMware admin should know. First i am going to give a quick guide how to set up and configure your environment. Later i am going to show the new features of PowerCLI 5.1 and PowerShell 3.0.

How to set up PowerCLI 5.1 with PowerShell 3.0

Download and Install PowerShell 3
If you're running Windows 8 already, you can skip this step. Windows 8 comes with PowerShell 3 out of the box. If you're running Windows 7 you have to download and install the Windows Management Framework (WMF) 3.0:

You have to choose the version that correlates your OS:
Windows 7 Service Pack 1 64-bit versions: WINDOWS6.1-KB2506143-x64.MSU
Windows 7 Service Pack 1 32-bit versions: WINDOWS6.1-KB2506143-x86.MSU
Windows Server 2008 64-bit versions: WINDOWS6.0-KB2506146-x64.MSU
Windows Server 2008 32-bit versions: WINDOWS6.0-KB2506146-x86.MSU

Download and install vSphere PowerCLI 5.1
A registration might be required, but it's the vSphere PowerCLI is free. The current version at the moment is VMware-PowerCLI-5.1.0-793510.

Set ExecutionPolicy
For security, you can't run PowerShell scripts by default. You have to set the ExecutionPolicy. That has to be done as Administrator. Right-Click Windows PowerShell, select "Run as Administrator..." and enter:

Set-ExecutionPolicy RemoteSigned

Create a Profile
Open PowerShell again with your user to configure the profile. The profile is a configuration file that is loaded when you start the PowerShell. It is usually located at C:\Users\<USER>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

Create and open your profile with:

New-Item -Path $Profile -ItemType file -Force
notepad $profile

Notepad will open with an empty file. Enter the following command and save the file. The "SilentlyContinue" will prevent the error message "Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core because it is alerady added. Verify the name of the snap-in.":

Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"

The PowerShell is now ready to go!
Connect to your vCenter Server and run your first command to get a list of virtual machines managed by the vCenter Server:

Connect-VIServer <VCENTER> -User <USER> -Password <PASSWORD>

What's new with PowerCLI 5.1

Datastore Cluster Support
You can create and modify Datastore Clusters and pass Datastore Clusters to the Datastore parameters. The Datastore Cluster Support was really missing in PowerCLI 5.0 and it is really great that is is now fully supported. Example:

#Create a Datastore Cluster "Gold" within the "Hamburg" Datacenter
New-DatastoreCluster -Name Gold -Location Hamburg

#Set the SDRS policy to Fully Automated
Set-DatastoreCluster -DatastoreCluster Gold -SdrsAutomationLevel FullyAutomated

#Move the LUN01 Datastore to the SDRS Cluster Gold
Move-Datastore LUN01 -Destination Gold

Added support for creating linked clones
You can create linked clones with PowerCLI:

#Usage: New-VM -Name <NAME> -VM <SOURCE-VM> -LinkedClone -ReferenceSnapshot <SNAPSHOT-NAME> -ResourcePool <RESOURCEPOOL> -Datastore <DATASTORE>
New-VM -Name xp2 -VM xp -LinkedClone -ReferenceSnapshot Snapshot1 -ResourcePool Management -Datastore LUN01

vCloud Director Support
There are plenty new cmdlets for the vCloud Director but this is not the scope of this post.

What's new with PowerShell 3.0

Where statement is simplified
The curly brackets ({}) and  the placeholder ($_.)  are no longer required. This will make the script more easy to read. Let's do a quick example. All the following commands are doing the same:

#PowerShell 2.0 Style
get-vm  | Where-Object {$_.PowerState -eq "PoweredOff"}

#PowerShell 3.0 Style
get-vm  | Where-Object PowerState -eq "PoweredOff"
get-vm  | ? PowerState -eq "PoweredOff"

The new Integrated Scripting Engine (ISE) has a auto-complete feature that saves time, and reduces typos:

PowerShell 3.0 Intellisense
The new PowerShell does live syntax checking. When you make mistake it will underline the error in red and gives a suggestions to correct the error:

Explore cmdlets
The cmdlet called Show-Command gives beginners a great opportunity to research and play around with the syntx of cmdlets. Show-Command is a GUI for creating commands:

New Out-GridView Parameter
The Out-Gridview cmdlet now has the -passthru parameter. This gives us a possibility to interact with the user. The user can choose which objects he want's to get processed by the script. Example:

# Start virtual machines selected by the user
Get-VM | Out-GridView -passthru | Start-VM

Leave a Reply

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