Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

Getting started with PowerShell and VMware vSphere

  • November 2, 2017
  • 8 min read
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.

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:

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

Create VM from a template via PowerCLI

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.

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

Hey! Found Romain’s article helpful? Looking to deploy a new, easy-to-manage, and cost-effective hyperconverged infrastructure?
Alex Bykovskyi
Alex Bykovskyi StarWind Virtual HCI Appliance Product Manager
Well, we can help you with this one! Building a new hyperconverged environment is a breeze with StarWind Virtual HCI Appliance (VHCA). It’s a complete hyperconverged infrastructure solution that combines hypervisor (vSphere, Hyper-V, Proxmox, or our custom version of KVM), software-defined storage (StarWind VSAN), and streamlined management tools. Interested in diving deeper into VHCA’s capabilities and features? Book your StarWind Virtual HCI Appliance demo today!