Getting started with PowerShell and VMware vSphere

Posted by Romain Serre on November 2, 2017
Share on Facebook0Share on Google+0Share on LinkedIn5Share on Reddit72Tweet about this on Twitter0
5/5 (1)
5/51

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

Since a few time, 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 an Internet Access. Then run:

install PowerCli from the PowerShell gallery

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.

Update Module Name with 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!

PowerCLI command

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:

Connect to VMware vCenter via PowerCLI

Get information from vCenter

Now that you are connected to the vCenter, you can gather information. First, I get information about Virtual Machine with the command Get-VM.

Get information from vCenter via PowerCLI

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

get the information about VM Host connected to the vCenter

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.

get information about vSAN configuration via PowerCLI

Create VM from a template

Get 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 hundred VMs from these templates. The fastest way is to use PowerCLI. Below, you can see that I get VM template from VMware vCenter.

Create VM from a template via PowerCLI

Then I get the VM Host inside the Cluster-ESX01 cluster, I sort them by memory usage and then I select the host with less memory usage. Next, I create a VM in this VM Host with the template w2016-Core-v1.0.

create a VM in the VM Host with the template

Next, I’m connecting to the vCenter and as you can see, the VM is creating.

vSphere Client VM summary

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 baseline and the compliance status of your VMHosts.

Update a cluster from a baseline via PowerCLI

Then you can get your baseline and add it in a variable. Then run:

get the baseline and add it in a variable via PowerCLI

In the below screenshot, you can see that the cluster is updating.

vSphere Client cluster infomation

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.

Related materials:

Views All Time
14
Views Today
29

Please rate this

To download the software products, please, make your choice below. An installer link and a license key will be sent to the e-mail address you’ve specified. If you consider StarWind Virtual SAN but are uncertain of the version, please check the following document Free vs. Paid. The recent build of Release Notes. A totally unrestricted NFR (Not For Resale) version of StarWind Virtual SAN is available for certain use cases. Learn more details here.



Return to all posts

Introducing Microsoft ‘Project Honolulu’
Looking at the human factors in security breaches
The following two tabs change content below.
Romain Serre
Romain Serre
Senior consultant at Exakis
Romain Serre works in Lyon as a Senior Consultant. He is focused on Microsoft Technology, especially on Hyper-V, System Center, Storage, networking and Cloud OS technology as Microsoft Azure or Azure Stack. He is a MVP and he is certified Microsoft Certified Solution Expert (MCSE Server Infrastructure & Private Cloud), on Hyper-V and on Microsoft Azure (Implementing a Microsoft Azure Solution).