Advertise on warmetal.nl!
Click for more information
about advertising here.

Did you find this website useful? Did I save you a lot of time?
Please consider donating to support this site:

 

Getting Started with PowerShell and vSphere PowerCLI

Getting Started with vSphere PowerCLI.

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>

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

Resources

Discussion

Enter your comment:
 
vspherepowercli.txt · Last modified: 2012/01/03 09:42 by sjoerd