The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 23 Jun 2011

Basic / Introductionary

Tags: ,


VMware’s support of PowerShell is laudable, personally I’ve tried (and failed) to become au-fait with Perl too many times to recall, so being able to script for your virtual infrastructure in PowerShell has been a god send for me.  If you know nothing of PowerShell, don’t let that put you off, nor did I before I started PowerCLI scripting.  There are a wealth of support pages out there to help, and here is my attempt to get you on your way…

Before you attempt to get the PowerCLI work, make sure you’ve got PowerShell working, if not see Getting started with PowerShell.

Installation

Install the latest copy of software from VMware’s site

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 VCorESX -User username -Password password

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.  All the PowerCLI is, is an API to interact with the vCentre or ESX SOAP interface.

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

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:WINDOWSsystem32windowspowershellv1.0powershell.exe -PSConsoleFile "C:Program FilesVMwareInfrastructurevSphere PowerCLIvim.psc1" "& C:ScriptsESX-probe.ps1"

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

Powershell -PSConsoleFile "C:Program Files (x86)VMwareInfrastructurevSphere PowerCLIvim.psc1" "& C:ScriptsESX-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 of vCentre (or ESX) 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).  Or you can just import the self-signed certificates onto your machine.

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 SettingsAll UsersApplication DataVMwareVMware VirtualCenterSSLrui.crt
    • Default location on Win2k8: C:ProgramDataVMwareVMware VirtualCenterSSLrui.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

Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.