Since some time, VMware provides PowerCLI which is a set of modules for VMware vSphere. Except if you were in a cave last 10 years, you should know that PowerShell is a powerful scripting language. Initially, PowerShell enabled to manage only Windows Workstation or Server, but since sometimes, a lot of vendors make their own modules to manage their solutions (such as Veeam, VMware and so on). Moreover, PowerShell is available on Linux.
For my job, I always use PowerShell. I’m a lazy guy, and if I have to make something two times, I make a script. This is the same thing for VMware vSphere. In this topic, we’ll see how to connect to vCenter and some commands to start.
Install or update PowerCLI
After a while, VMware has published the PowerCLI in the PowerShell gallery. In this way, you don’t have to install an application anymore to use PowerCli.
N.B: To follow this part, you need PowerShell v5 at least. If you are not using Windows 10 or Windows Server 2016, you can install the latest Windows Management Framework.
To install PowerCli from the PowerShell gallery you need Internet Access. Then run:
1 |
Install-Module -Name VMware.PowerCLI |
If you have already installed PowerCLI from the PowerShell gallery, you can run the following command:
N.B: If you have installed the PowerCLI from executable, you need to remove it and install PowerCLI with the above command.
1 |
Update-Module -Name VMware.PowerCLI. |
When PowerCLI is installed, you can run the below command. You can see that 620 cmdlet exists for VMware vSphere. That’s a lot!
Connect to VMware vCenter
To manage VMware vSphere you have to connect to VMware vCenter. First, run a Get-Credential to store in variable credentials to connect to vCenter. Then run the following command:
1 |
Connect-VIServer -Server <vCenter FQDN> -Credential <Credential variable> |
Get information from vCenter
Now that you are connected to the vCenter, you can gather information. First, I get information about a Virtual Machine with the command Get-VM.
You can also get information about VM Host (ESXi) connected to the vCenter. You can get a lot of useful information by running, for example, Get-VMHost | FL *.
Because my cluster is running with vSAN, I can also get information about vSAN configuration. Cmdlet provided by PowerCLI enables to manage VMware vSphere included VMware NSX, VMware vSAN, and so on.
Create a VM from a template
Getting information is really nice, but you can also create a VM. For example, you have created one or several templates and now you want to create a hundred VMs from these templates. The fastest way is to use PowerCLI. Below, you can see that I get a VM template from VMware vCenter.
Then I get the VM Host inside the Cluster-ESX01 cluster, sort them by memory usage, and then select the host with less memory usage. Next, I create a VM in this VM Host with the template w2016-Core-v1.0.
1 2 3 |
$VMHost = (Get-VMHost |? Parent -like <Cluster Name>| Sort-Object MemoryUsageGB | Select -First 1).Name New-VM -Name <VM Name> -Template <Template Name> -VMHost $VMHost |
Next, I’m connecting to the vCenter and as you can see, the VM is creating.
Update a cluster from a baseline
Another example of PowerCLI regards the updating of your VMHosts. By running Get-Baseline and get-Compliance, you can get information about the baseline and the compliance status of your VMHosts.
Then you can get your baseline and add it in a variable. Then run:
1 |
Remediate-Inventory -Entity <VMHost, Cluster name> -Baseline $Baseline. |
In the below screenshot, you can see that the cluster is updating.
Conclusion
PowerShell is a powerful scripting language. It’s great that VMware has made modules to manage VMware vSphere, including VMware NSX and vSAN. Thanks to these modules, you can automate a lot of tasks such as the VM creation, the host deployment, and so on.