Objective 7.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI

Identify vSphere PowerCLI requirements

Directly from the vmware website for PowerCLI 5.1 release notes:

To use VMware vSphere PowerCLI, you need to have installed the following software:

  • Windows PowerShell 2.0
  • A supported version of .NET Framework
    • .NET Framework 2.0 with Service Pack 2
    • .NET Framework 3.0 or .NET Framework 3.0 with Service Pack 1, or Service Pack 2
    • .NET Framework 3.5 or .NET Framework 3.5 with Service Pack 1

Identify Cmdlet concepts

  • A cmdlet is a lightweight command that is used in the Windows PowerShell environment. The Windows PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The Windows PowerShell runtime also invokes them programmatically through Windows PowerShell APIs.
  • Windows PowerShell uses a verb-and-noun name pair to name cmdlets. For example, the Get-Command cmdlet included in Windows PowerShell is used to get all the cmdlets that are registered in the command shell. The verb identifies the action that the cmdlet performs, and the noun identifies the resource on which the cmdlet performs its action.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms714395(v=vs.85).aspx

 

Identify environment variables usage

Environment Variables are usually used as shortcuts to common paths that are found on a computer. Environment Variables are stored in the  PSDrive Env.

  • To find which environment variables are available to use, at a powershell prompt type “Get-ChildItem Env:” To which the output will look similar to this:

powersh01

  • In order to use the listed Environement Variables “$env:” should be place in the front

Install and configure vSphere PowerCLI

  • Download the latest version of vSphere PowerCLI from the VMware Web site.
  • Navigate to the folder that contains the vSphere PowerCLI installer file you downloaded and double-click the executable file.
  • If the installation wizard detects an earlier version of vSphere PowerCLI on your system, it will attempt to upgrade your existing installation.
  • On the Welcome page, click Next.
  • On the VMware Patents page, click Next.
  • Accept the license agreement terms and click Next.
  • On the Custom Setup page, select the components that you want to install.
  • Option Description
    • vSphere PowerCLI Installs a set of cmdlets for managing vSphere features. This vSphere PowerCLI component is mandatory and selected by default.
    • vCloud Director PowerCLI Installs a set of cmdlets for managing vCloud Director features.
  • (Optional) On the Custom Setup page, click Change to select a different location to install
  • vSphere PowerCLI.
  • Click Next.
  • On the Ready to Install the Program page, click Install to proceed with the installation.
  • Click Finish to complete the installation process.

 

Install and configure Update Manager PowerShell Library

You can download the Update Manager PowerCLI installer package from the product landing page.

  • To install the Update Manager PowerCLI
  • Start the Update Manager PowerCLI installer.
  • Click Next in the Welcome page to continue with the installation.
  • Read and accept the license agreement terms.
  • Click Install.
  • Click Finish to complete the installation process.

Use basic and advanced Cmdlets to manage VMs and ESXi Hosts

  • For more examples please refer to the PowerCLI User’s Guide
  • After connecting to the vCenter Server with “connect-viserver $vcenterservername”, you can retrieve information about the vms and hosts by using the two basic commands “Get-VM and Get-VMHost”.
  • Directly from the Get-Help of set-vm and set-vmhost

 

C:\PS>Get-VM -Location ResourcePool01 | Set-VM -MemoryGB 2 -NumCPU 2
-Upgrades the memory and CPU count of the virtual machines in ResourcePool01.

C:\PS>Set-VM $vm -Name “Web Server” -GuestID winNetStandardGuest -Description “Company’s web server”
-Changes the name, description, and guest ID of the specified virtual machine.

C:\PS>Set-VMHost -VMHost Host -State “Disconnected”
-Resets the state of the Host virtual host to disconnected.

Use Web Service Access Cmdlets

  • The get-view and the get-viobjectbyviview are considered the Web Services Access Cmdlets
  • Both of these cmdlets function to access/manipulate the underlying .Net objects or PowerCLI
  • Get-View takes a Powershell VIObject and converts it to a vSphere .Net View Object. Get-VIObjectByVIView does just the opposite.

 

Get-View -viewtype “VirtualMachine”
-Returns the vSphere .Net view objects of all the virtual machines

$view=Get-View -viewtype “VirtualMachine”
Get-VIObjectByVIView $view
-Stores the previous example’s result in the variable $view, then converts it back to the standard PowerShell VIObject

Use Datastore and Inventory Providers

  • The Datastore and Inventory Providers allow you to browse the  the default datastore drives
  • Using the cmdlet Get-PSDrive you should see two psdrives listed, vmstore and vmstores
    • VMstore is the last connected vCenter Server, and VMStores is the currently connected vCenter Server
  • PSDrives are great because they operate much like one would navigate an operating system via commandline.

powersh02

 

Given a sample script, modify the script to perform a given action

Just going to have to hope for the best!