In this article, we will understand what VMware PowerCLI is, why to use it, how to install and configure it, and how to manage vSphere.
Let’s imagine you have an ESXi machine, and you want to install a virtual machine on it, Linux Ubuntu, for example.
How to do that?
vSphere Web Client is the answer. It replaces vSphere Client (only compatible with Windows systems) with a web interface, becoming cross-platform and allowing us to manage the entire virtual infrastructure on ESXi systems.
But vSphere Web Client is not the only way.
There is a powerful, easy-to-use system that allows users to manage ESXi and VMware vSphere servers via command line: it’s about PowerCLI, a Windows PowerShell command-line interface.
Thanks to the many commands provided (more than 500) PowerCLI allows you to automate with scripts even complex aspects of vSphere management, such as hardware, VM or network.
Being an extension of PowerShell, PowerCLI was created for Windows operating systems, but now it is also available for macOS and Ubuntu.
How to prepare the system
The minimum infrastructure to test PowerCLI capabilities is composed of:
– an ESXi machine
– a machine (Linux or any other os) with which to connect to the above-mentioned ESXi machine with vSphere Web Client
– a virtual machine (Linux or any other os) created with vSphere Web Client
– a windows machine for VMware PowerCLI installation
In our case, our Esxi machine looks like this:
There is no interface, but only the server IP to connect to.
From the Linux machine, it is sufficient to type the ESXi machine IP address in the browser and enter the credentials chosen during the host installation.
Once logged in, you are ready to manage the entire virtualization system.
If you want to create a new virtual machine, choose the menu “Virtual Machine” and select “Create/Register VM” where you can set all the options you want.
Despite the ease of a web interface like vSphere web client, thanks to PowerCLI you can automate all the actions that are done here manually, one at a time.
In this way, you can optimize the time and reduce errors that may occur in a complex virtual machine infrastructure management.
How to install PowerCLI
Originally PowerCLI was installed as an application and was then launched by the appropriate command.
Now, since version 6.5.1, PowerCLI has been incorporated with a suite of modules within the PowerShell prompt itself.
This makes it even easier to use.
Then open a PowerShell prompt (as administrator)
and install the modules with the following command:
$> Install-Module VMware.PowerCLI -Scope CurrentUser
In this way, the VMWare.PowerCLI installation modules will be downloaded and saved in the dedicated folder.
The -Scope parameter is used to make PowertCLI modules available to the current user (CurrentUser) or all users (AllUsers).
Once the installation is finished, you can see the list of modules with the following command:
$> Get-Module -ListAvailable -Name VMware*
Before connecting to the ESXi server, you must set the InvalidCertificateAction parameter in the PowerCLI configuration.
This determines the action to take when attempting server access for which the certificate cannot be guaranteed to be secure.
Setting it to “Prompt” will allow you to decide what to do each time before continuing.
$> Set-PowerCLIConfiguration -InvalidCertificateAction Prompt
For the ESXi host connection, run the following command:
$> connect-viserver <ip>
where <ip> is to be replaced with the ESXi machine IP
And once you accept the certificate and enter your credentials,
the system responds in this way:
At this point, it is possible to perform operations directly on the ESXi host using the PowerCLI commands (cmdlets).
Most used commands
On the official VMware page, you can find a list of all the commands that can be used with PowerCLI.
Below is a list of the most common and used commands.
For virtual machine management on ESXi servers you can use commands such as
$> Get-VM -Location <Ip>
to display the list of VMs and their status.
$> Start-VM -VM my_vm -Confirm -RunAsync
to activate the virtual machine.
$> New-VM -Name new_vm
to create a new virtual machine.
$> Remove-VM new_vm -DeletePermanently
to remove a virtual machine permanently.
But it is also possible to do more complex operations, such as backing up and restoring the configuration of the ESXi host.
Backing up and restoring an ESXi configuration
You know very well how important it is always to be able to count on backups, for security reasons, but also to save time when you need to use a configuration that has been already tested.
Even in the case of an ESXi server, therefore, it may be necessary to make a backup of its configuration.
With PowerCLI, you can do this with a few simple commands.
$> get-vmhost | get-VMHostFirmware -BackupConfiguration -DestinationPath <path>
In <path> is specified where you want to save the backup.
Then, if you want to restore this configuration, there is another command to use, and it is as follows:
$> Set-VMHostFirmware -VMHost <ip> -Restore -Force -SourcePath <sourcepath>
The system is restarted without any confirmation.
How to uninstall PowerCLI
If you want to uninstall VMware PowerCLI, in order to install a new version or simply because you no longer need it, the commands to follow are as follows:
$> Get-module VMware.* -listAvailable | Uninstall-Module -Force
To test the deletion of any files related to the installation:
$> Get-module -listAvailable -Name VMware*
Maintaining a virtualization infrastructure, it’s often necessary to repeatedly perform actions and commands, which can also lead us to make mistakes.
We have seen how PowerCLI allows, thanks to the potential of the command line interface, to execute simple commands that, saved in special scripts, can be used to avoid errors and boring and repetitive tasks.