Automating with PowerCLI

The most common automation tool provided by vSphere is PowerCLI, a command-line and scripting tool built on Windows PowerShell that provides cmdlets used for managing and automating vSphere and other VMware products.

The installation process of PowerCLI has been simplified and requires running a command from the PowerShell console. To install PowerCLI, an additional Microsoft component, PackageManagement PowerShell Modules Preview (download available at the URL https://www.microsoft.com/en-us/download/confirmation.aspx?id=51451), must be installed in your system.

To install PowerCLI, follow this procedure:

  1. Open the PowerShell console and run the following command:
Install-Module -Name VMware.PowerCLI

If you get an error, the PackageManagement PowerShell Modules Preview is not installed in the system. Double-click PackageManagement_x64.msi to install.

  1. You are prompted to install a NuGet provider to interact with the NuGet-based repositories. Type Y, then press Enter. You may receive a warning that the used repository is untrusted; type Y and press Enter to install the module:
To install PowerCLI, the VMware.PowerCLI package must be installed in the PowerShell console
  1. To see the installed modules, run the following command:
Get-Module vmware* -listavailable

For example, using PowerCLI, you can quickly get the list of running VMs in a specific vCenter Server instance:

  1. Open the PowerShell console where the VMware PowerCLI module has been installed
  2. From the console, connect the vCenter Server instance to query, then run the following command (enter the login credentials when prompted):
Connect-VIserver –server VCSA_fqdn
  1. To get a list of VMs running in the selected vCenter Server instance, enter the following command:
Get-VM
Use of PowerCLI cmdlets can be used to query the vSphere environment

Remembering all PowerCLI commands and the correct syntax is not easy for most people and the documentation or the Internet connection are not always available to help. A useful cmdlet is available in PowerShell that provides information about a specific command—Get-help. To find the correct syntax to use with a specific cmdlet, Get-help helps you to find the information you need. For example, to find what parameters can be used with the cmdlet Get-VM to retrieve the list of running VMs, you can enter the following:

Get-help Get-VM
 The Get-help cmdlet allows you to find the correct syntax to use for a specific command

You get a brief explanation of the command, the syntax to use, and the description. If you append -example at the end of the command, the system also displays examples of how to use the command. You can pipeline multiple PowerShell cmdlets to build a script in a single line of code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.203.172