Table of Contents
Getting Started with PowerShell and vSphere PowerCLI
Summary: How to get started with vSphere PowerCLI.
Date: 6 November 2012
Refactor: 1 May 2025: Checked links and formatting.
Prerequisites
- .NET 2.0 SP1 or higher
- Windows PowerShell 1.0 or higher
- For Windows Server 2008, it's an feature that can be installed through server manager. On R2 it's called Windows PowerShell Integrated Scripting Environment (ISE).
How to Start Windows PowerShell
Note that you can check powershell installation and version like this:
- In Windows Vista, Windows Server 2008, and later versions of Windows, to start Windows PowerShell from the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click the Windows PowerShell item.
- In Windows XP and Windows Server 2003, to start Windows PowerShell from the Start menu, click Start, click Programs, click Accessories, click the Windows PowerShell folder, and then click the Windows PowerShell item.
- How to Start Windows PowerShell ISE
- Windows PowerShell Integrated Scripting Environment (ISE) is a new host application that lets you run commands and write, test, and debug scripts in a friendly, syntax-colored, environment. For more information about Windows PowerShell ISE, see Windows PowerShell Integrated Scripting Environment (ISE) Help.
- In Windows Vista, Windows Server 2008, and later versions of Windows, to start Windows PowerShell ISE, from the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click the Windows PowerShell ISE item.
- In Windows XP and Windows Server 2003, to start Windows PowerShell from the Start menu, click Start, click Programs, click Accessories, click the Windows PowerShell folder, and then click the Windows PowerShell ISE item.
You can request the powershell version by typing this command in the powershell:
PS C:\Documents and Settings\admin> get-host Name : ConsoleHost Version : 2.0 InstanceId : 8b6499f9-a283-4c44-9adf-f950fddb0def UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace
Download PowerCLI
Getting the latest version of vSphere PowerCLI:
Installing
Installing is just a next next job, nothing special.
Note: You might get a warning that the execution policy is still set to restricted. This can be ignored during the installation and resolved through the tip regarding this in the getting started section.
Getting Started
First step is to set the execution policy from restricted to remotesigned. To do so, rightclick the PowerCLI shortcut on your desktop and select “Run as Administrator”. When started up (this could take some time) issue the command below:
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y PS C:\Windows\system32>
Note: UseSet-ExecutionPolicy RemoteSigned -force
to suppress the confirmation.
When finished close the shell by typing exit and restart it with normal permissions.
Welcome to the VMware vSphere PowerCLI! Log in to a vCenter Server or ESX host: Connect-VIServer To find out what commands are available, type: Get-VICommand To show searchable help for all PowerCLI commands: Get-PowerCLIHelp Once you've connected, display all virtual machines: Get-VM If you need more help, visit the PowerCLI community: Get-PowerCLICommunity Copyright (C) 1998-2010 VMware, Inc. All rights reserved.
First step now is to connect to your vCenter server:
[vSphere PowerCLI] C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Connect-VIServer vcenter WARNING: There were one or more problems with the server certificate: * The X509 chain could not be built up to the root certificate. * The certificate's CN name does not match the passed value. Name Port User ---- ---- ---- vcenter 443 AD\sjoerd
Now you can start using PowerCLI. As a first command I could recommend to just try to see al your VMs:
[vSphere PowerCLI] C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VM Name PowerState Num CPUs Memory (MB) ---- ---------- -------- ----------- xxxxxxxxxxxxxxxx PoweredOn 4 4096 xxxxxxxxxxxxxxxxxx PoweredOff 2 768 ...<cut>...
If you need any help on a command try the Get-Help command:
[vSphere PowerCLI] C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> get-help Get-View NAME Get-View SYNOPSIS Returns the vSphere .Net view objects that correspond to the specified search criteria. SYNTAX Get-View [-VIObject] <VIObject[]> [-Property <String[]>] [<CommonParameters>] Get-View [-Server <VIServer[]>] [-Id] <ManagedObjectReference[]> [-Property <String[]>] [<CommonParameters>] Get-View [-Server <VIServer[]>] [-SearchRoot <ManagedObjectReference>] -ViewType <Type> [-Filter <Hashtable>] [-Pro perty <String[]>] [<CommonParameters>] DESCRIPTION Returns the vSphere .Net view objects that correspond to the specified search criteria. The cmdlet retrieves the vS phere .NET view objects specified by their IDs or by their corresponding vSphere inventory objects (VIObject). A vi ew object ID is a <type>-<value> string. For objects with constant names such as AlarmManager and ServiceInstance, the ID format is <type> (see the examples). For more information about vSphere.NET objects, see the vSphere SDK for .NET Developer's Guide. RELATED LINKS Online version: http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Get-View.html Get-VIObjectByVIView REMARKS To see the examples, type: "get-help Get-View -examples". For more information, type: "get-help Get-View -detailed". For technical information, type: "get-help Get-View -full".
To see all possible PowerCLI commands:
Get-VICommand
Slow Startup PowerCLI
I experienced a really slow startup from PowerCLI. It had something to do with internet access and checking the certificates. I disabled this through:
- Open the Control Panel → Go To Internet Options → Go to the Advanced tab → Go to the Security Section
- Uncheck the “Check for publisher's certificate revocation” checkbox
Adding More Modules to PowerShell
I downloaded the NetApp PowerShell Module DataONTAP from here. You can use it by issuing this command inside powershell:
import-module d:\sjoerd\dataontap
Connect Error Connect-VIServer
On some server I got this error:
PowerCLI C:\Windows\system32> connect-viserver vcenter Connect-VIServer : 6/11/2012 11:40:45 AM Connect-VIServer Could not connect using the requested protocol. At line:1 char:17 + connect-viserver <<<< esmprda01 + CategoryInfo : ObjectNotFound: (:) [Connect-VIServer], ViServerConnectionException + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_ProtocolError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer
The solution is to disable the proxy settings:
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -Confirm:$False
Certificate Error Connect-VIServer
You can make the certificate errors like this one:
WARNING: There were one or more problems with the server certificate: * A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust provider. * The X509 chain is not valid due to an invalid time value, such as a value that indicates an expired certificate. * The certificate's CN name does not match the passed value. Certificate: [Subject] E=support@vmware.com, CN=VMware, OU="VMware, Inc.", O="VMware, Inc.", L=CA, S=CA, C=US [Issuer] E=support@vmware.com, CN=VMware, OU="VMware, Inc.", O="VMware, Inc.", L=CA, S=CA, C=US [Serial Number] 00F0B7F87A68C25FE0 [Not Before] 12/21/2007 11:01:20 AM [Not After] 12/20/2009 11:01:20 AM [Thumbprint] 436D0B76FAEE185BFFDF4625804BC685B9DA9486 The server certificate is not valid. WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN A FUTURE RELEASE. To ensure scripts are not affected by the change, use Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction option.
or this one:
WARNING: There were one or more problems with the server certificate: * A certification chain processed correctly, but terminated in a root certificate which isn't trusted by the trust provider. * The X509 chain is not valid due to an invalid time value, such as a value that indicates an expired certificate. * The certificate's CN name does not match the passed value.
go away like this:
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False
So you can combine the previous two settings like this:
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -ProxyPolicy NoProxy -Confirm:$False