Getting Started (PowerCLI)

From vwiki
Revision as of 21:36, 25 October 2011 by Sstrutt (talk | contribs) (Move of content from original PowerCLI/VIToolkit page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

On the first run you need to allow the Toolkit 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

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.

Be aware that PowerShell commands generally return objects, rather than text, and that 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                : esx1.domain.com
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                : vserver2.domain.com
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

Installed Version

To determine the version of PowerCLI currently installed use;

  • Get-PowerCLIversion

Scheduling

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...

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

Certificates

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 http://www.vmware.com/pdf/vi_vcserver_certificates.pdf 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