Getting Started (PowerCLI)

From vwiki
Jump to navigation Jump to search

The Basics

Before being able to use any the following script you need to install...

  • Windows PowerShell V1
  • Windows Powershell V2 - Available for Win7 or Win2008
  • PowerCLI - Formerly known as the VI Toolkit
    • Note that versions are broadly speaking, backwards compatible. So even if you've only got the v4 vCentre's and ESX's, install the latest v5 version of PowerCLI

Set Execution Policy

On the first run you need to allow PowerCLI to run properly by running (you need to run this command as an administrator, so if you're using Windows 7, for example, you'll need to start the PowerShell console as an administrator, regardless of whether you're currently logged in as an admin)...

 Set-ExecutionPolicy RemoteSigned

If you don't do this you'll get the following error when the PowerCLI Snap-In attempts to load into your session.

  • ...Initialize-PowerCLIEnvironment.ps1 cannot be loaded because the execution of scripts is disabled on this system. ...

If it takes a long time (minutes) for the PowerCLI console to start, see Slow Start-Up.

Connect to Infrastructure

Connect to the Virtual Centre (or ESX) server using the following command using your normal username and password (same as you'd use to log into the VI Client). You will need access to the servers on TCP 443.

 Connect-VIServer -Server <server> -User <user> -Password <pass>

Once connected you can do real work with the PowerCLI, to see what's available, login to https://server/mob. This is effectively where you end up once you've completed the Connect-VIServer command.

Try some of the following...


Be aware that PowerShell commands generally return objects, rather than simple variables containing text or numbers. So the textual representation of the return object is often abbreviated for simplicity. To see the entire return for a command, pipe the result into Format-List. To complicate matters further, some return objects contain further objects, see examples below

[vSphere PowerCLI] E:\> get-vm -name "vserver1"

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
vserver1               PoweredOn  1        756

[vSphere PowerCLI] E:\> get-vm -name "vserver1" | Format-List *

PowerState          : PoweredOn
Description         :
Guest               : VMware.VimAutomation.Client20.VMGuestImpl
NumCpu              : 1
MemoryMB            : 756
CDDrives            : {CD/DVD Drive 1}
FloppyDrives        : {Floppy Drive 1}
HardDisks           : {Hard Disk 1}
NetworkAdapters     : {Network Adapter 1}
Host                :
HostId              : HostSystem-host-301
HARestartPriority   : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-25136
Name                : vserver1

[vSphere PowerCLI] E:\> get-vm -name "vserver2" | Format-List *

PowerState          : PoweredOff
Description         :
Guest               : VMware.VimAutomation.Client20.VMGuestImpl
NumCpu              : 2
MemoryMB            : 2048
CDDrives            : {CD/DVD Drive 1}
FloppyDrives        : {Floppy Drive 1}
HardDisks           : {Hard Disk 1}
NetworkAdapters     : {Network Adapter 1, Network Adapter 2, Network Adapter 3}
Host                :
HostId              : HostSystem-host-662
HARestartPriority   : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-697
Name                : vserver2

[vSphere PowerCLI] E:\> get-vm -name "vserver2" | ForEach-Object {$_.NetworkAdapters}

MacAddress       : 00:50:AB:CD:40:59
WakeOnLanEnabled : True
NetworkName      : Some_Network
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4000
Name             : Network Adapter 1

MacAddress       : 00:50:AB:CD:55:40
WakeOnLanEnabled : True
NetworkName      : Another_Network
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4001
Name             : Network Adapter 2

MacAddress       : 00:50:AB:CD:56:da
WakeOnLanEnabled : True
NetworkName      : Yet_Another_Network
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4002
Name             : Network Adapter 3

Import into PowerShell

You can just run PowerCLI by running the shell directly. In order to be able to use PowerCLI CmdLets in a normal PowerShell session you need to import the module in...

Add-PSSnapin VMware.VimAutomation.Core

Installed Version

To determine the version of PowerCLI currently installed use;



To schedule a script to run you need to include the PowerCLI VIM console file so that PowerCLI Cmd-lets are available to your script. The command should use the following structure...

  • Powershell.exe -PSConsoleFile <vim.psc1> &<script.ps1>

For example, on a WinXP or Win2k3 (32bit) machine you might have something like...

  • C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& C:\Scripts\ESX-probe.ps1"

Or on a Win7 or Win2k8 (64bit) machine you might have something like...

  • Program/script: Powershell
  • Add arguments (optional): -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& C:\Scripts\ESX-probe.ps1"

Log Output

To capture the Standard Output and Error Output streams from the script being run, append something similar the following to the end of your command line

  • > script-stdout.log 2> script-errout.log

This may capture more than you would be able to see from a Transcript of the script run (see PowerShell Logging).

Force 32-bit

To ensure that a script runs in a 32-bit environment rather than 64-bit, you need to run with 32-bit version of PowerShell, so you'd use the following in the Program/script box...

  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe


A regular irritation is the certificate warnings that pop-up whenever you connect to a Virtual Centre. To completely fix you need update the default, out of the box, self-signed certificates that are created during the install with proper certs that can be tracked back to a fully trusted root (see for further info).

To install a VC's certificate on your client machine (or any machine you want to run PowerCLI from and avoid the warnings)...

  1. Map a drive, or do whatever so you can access the VC's cert from the client machine
    • Default location on Win2k3: C:\Documents and Settings\All Users\Application Data\VMware\VMware VirtualCenter\SSL\rui.crt
    • Default location on Win2k8: C:\ProgramData\VMware\VMware VirtualCenter\SSL\rui.crt
  2. On the client machine, start the Certificate Manager (run certmgr.msc )
  3. Right-click over Trusted Root Certificate Authorities, and click All Tasks > Import...
  4. Use the wizard to add the certificate
  5. Once added, go to the certificate's Properties and put your VC's hostname in the Friendly Name field

Alternatively, and to suppress all warning messages during a connect, use the following syntax...

Connect-VIServer -Server hostname -WarningAction SilentlyContinue