VCAP-DCA5 Objective 8.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI

Hey vNoob here; Helping out Jason and Josh by doing an objective for them. Hope it is helpful

 Objective 8.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI 

Knowledge

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

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

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


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:
  • In order to use the listed Environement Variables “$env:” should be place in the front

For example

Skills and Abilities

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.
  • “Using the Web Service Access cmdlets for low-level VMware vSphere management requires some knowledge of both PowerShell scripting and the VMware vSphere API.” quote

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.(Case-Sensitive)


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

Let’s look at an example we used earlier

Get-VM -Location ResourcePool01 | Set-VM -MemoryGB 2

  • This example is great if we only want to ever change the memory allocated to VMs in ResourcePool01, but if we know this will be something we are going to use over and over again to individual VMs, let make it a bit more usable.
  • Put this into a txt file

Param($vm, [int]$value)
Get-VM $vm | Set-VM -MemoryGB $value

  • Name the text file something easy like “setvmgb.ps1”  The ps1 extension denotes a powershell file. The param($vm) states that $vm will be our parameter for the vm used, and [int]$value will be the integer used. This makes the script much more usable and versile. So let’s try it out


./setvmgb.ps1 -vm myfirstvm -value 3
-Will set the vm “MyFirstVM” to have 3GB

./setvmgb.ps1 -vm “myfirstvm”,”mysecondvm” -value 2
-Will set “MyFirstVM” and “MySecondVM” to have 2gb

./setvmgb -vm (Get-vm -location ResourcePool01) -value 2
-With “Get-vm -location ResourcePool01” we grab all the VMs in that resource pool, and set them to 2GB