PowerCLI Tutorial: A Guide for Newbies Managing VMware

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

VMware PowerCLI is a user interface for the VMware vSphere Command-Line Interface (vCLI) to automate and orchestrate infrastructure provisioning, configuration, monitoring of virtual machines and data center environments. This guide will introduce you to vmware-powercli from scratch so that you can create your first PowerCLI scripts quickly.

The “PowerCLI Tutorial: A Guide for Newbies Managing VMware” is a guide that helps newbies get started with PowerCLI. It covers the basics of what PowerCLI is and how to use it. Read more in detail here: vmware powercli.

PowerCLI Tutorial: A Guide for Newbies Managing VMware

VMWare’s PowerCLI is quickly becoming the de facto interface for hypervisor virtualization managers using ESXi-compatible hardware. Learning how to use PowerShell and PowerCLI to handle VMware is a valuable addition to your toolset. You’ll learn how to accomplish precisely that in this PowerCLI lesson.

This guide will teach you how to:

  • the fundamentals of using PowerCLI to connect to VMware infrastructure
  • Get to know the cmdlets for interacting with VMware objects.
  • Give yourself the ability to explore by arming yourself with information.
  • Using PowerCLI, you can better administer VMware.

Let’s get started!

What You Should Know and Have

Before we go too far into this PowerCLI lesson, it’s always a good idea to double-check that both you and I are on the same page. For the sake of this post, I’ll assume that:

  • You know how to use PowerShell at a basic level. By no means do you need to be a PowerShell specialist, but whatever PowerShell knowledge you have will come in handy.
  • For connecting to and testing, you have a VMware ESXi host or vCenter appliance.
  • You’re using a Windows (or Mac / Linux) computer to accomplish your business.
  • You’ve already installed PowerCLI and are connected. If not, have a look at this PowerCLI setup guide.

I’ll be working with PowerCLI version 11.4.0. I’ll be using Windows PowerShell 5.1 on a Windows 10 workstation, but you’re free to use PowerShell 6 instead since they have feature parity for this course.

PowerCLI may be used to get information about ESXi hosts.

It’s a good idea to examine the physical hardware layer of our vSphere installation before diving into the virtual layer. Get-VMHost is a cmdlet provided by VMware PowerCLI that may be used to get specific information.

Run the following cmdlet when connected to your vCenter server or ESXi host.

Get-VMHostGet-VMHost

I am presented with some basic information about the hosts being managed since I am connected to a vCenter Server Appliance that is handling two distinct ESXi hosts. You may use Get-VMHost to examine the hardware capabilities of your virtualization hosts. Get-VMHost, on the other hand, does not return all of the information it can by default.

You may receive console output for all information about the specified hosts if you route the Get-VMHost cmdlet to Format-List.

Format-List | Get-VMHostFormat-List | Get-VMHost

You may even be more selective and just choose certain characteristics on the VMHost object you’re dealing with, such as:

PS51> Format-List | Get-VMHost -Property State,LicenseKey,Version

Using this more verbose output, you’ll have a better picture of the underlying hardware that’s powering your virtualized applications. The following is a list of some of the details:

  • Information about obtaining a license
  • Total CPU/Memory usage
  • Vendor model for hardware
  • Hostname in DNS

This may be assembled into a CSV file after evaluation, possibly using the Export-CSV cmdlet.

Using PowerCLI to inspect virtual machines

Let’s move on to the next section of this PowerCLI lesson, which is examining virtual hosts, or looking at which virtual machines are presently running on a certain ESXi host.

The Get-VM command is a useful tool for reviewing VM information.

PS51> Get-VMHost -Name <Host FQDN> | Get-VM

This cmdlet’s output will resemble the following:

PS51> Get-VMHost -Name <Host FQDN> | Get-VMPS51> Get-VMHost -Name | Get-VM

The preceding result shows a complete list of all presently operating VMs on a specific host. If you don’t specify an unique host, like I did above, you’ll get a table with information for each following host and the VMs on each.

You may use these cmdlets in the future if you need to know the number of virtual machines on a certain host or if you need to utilize VMware PowerCLI to acquire ESXi host information while diagnosing a problem. The reporting tools you have at your disposal are beginning to grow!

Using PowerCLI to inspect virtual switches

There is a wide set of networking cmdlets for every use case for those of us entrusted with establishing or managing the virtual networks of a vCenter cluster. If you want to inspect the virtual switches that are setup in your vSphere environment, type:

Your resultant list may be fairly long, depending on the number of virtual networks and virtual switches you have. As you can see in the diagram below, I only have one DSwitch, which is a Distributed Virtual Switch.

DSwitches facilitate the deployment of virtual switches and port groups across a variety of hosts with the same configuration in an Enterprise vSphere solution with multiple ESXi hosts in a cluster. This eliminates the time-consuming process of manually setting identical network configurations on each server, and it’s a terrific way to grow your cluster!

Get-VirtualSwitchGet-VirtualSwitch

Using PowerCLI, you can find VMs that are connected to a virtual network.

You could be wondering which VMs are linked to which networks when trying to narrow down the extent of a problem. You may use the Get-VirtualPortGroup command to find out. In this PowerCLI lesson, we’ll learn about this cmdlet.

A virtual network is what a port group is. Run Get-VirtualPortGroup without any arguments to see all port groups. After that, you should get a list of all virtual port groups in your vSphere environment.

Get-VirtualPortGroupGet-VirtualPortGroup

You can use the script below to discover all of the VMs in that port group (DPortGroup in my example). Make sure the value DPortGroup is replaced with the name of your Virtual Port Group.

PS51> Get-VM | Where-Object { ($PSItem | Get-NetworkAdapter | where {$_.networkname -match “DPortgroup”})}

We’re receiving a list of all virtual machines in this vCenter appliance with this one-liner, and then filtering using the Where-Object cmdlet to retrieve just those with a network name that fits our Port Group.

Obtaining VMs that only have a single port groupObtaining VMs that only have a single port group

You’ll be able to specify which computers are connected and configured for each network using this output.

When requested to discover and evaluate which VMware VMs are linked to a certain network, you will now be able to respond with a PowerCLI reporting solution.

Using PowerCLI to get information about the OS versions on your virtual machines

The majority of VMware’s administrative work is done at the virtual machine level. You’ll probably get a lot of requests for things like obtaining a list of all VM hard drive sizes or receiving guest OS versions for all of your servers, among other things.

At scale, these activities are time-consuming. With a few key cmdlets to add to your arsenal, PowerCLI can simplify these processes, so let’s go through them in this PowerCLI lesson.

“How many Ubuntu servers do we have in our VMware cluster?” you may have been asked at some point. You could have then wasted much too much time in vCenter trying to find a solution. By looking at the VM objects in vCenter and adding some PowerShell magic to the output, PowerCLI can speed up this process.

Take a look at the script below, which collects VM information. This sample makes use of the Get-View command, which we’ll go over in more detail later, but for now, know that it’s a more complex approach to get VMware object attributes. We’re collecting nested attributes that are the most readily obtained in this function in this example.

PS51> Get-VM | Sort-Object -Property Name | Get-View -Property @(“Name”, “Config.GuestFullName”, “Guest.GuestFullName”) | Select-Object -Property Name, @{N=”Configured OS”;E={$_.Config.GuestFullName}}, @{N=”Running OS”;E={$_.Guest.GuestFullName}}

The code above uses the PowerCLI Get-VM cmdlet to get a list of virtual machines, sorts the list using the PowerShell Sort-Object cmdlet, and then uses the PowerCLI Get-View cmdlet to get some of the object attributes.

When I run this in my environment, I get the following result. In vCenter, you can see the VM’s name, the Configured OS (which is how VMware’s virtual hardware interprets the guest operating system), and the actual Running OS (which represents the guest operating system).

VMs are found using a variety of parameters.VMs are found using a variety of parameters.

You won’t be able to see the Running OS value for replicant and Scriptrunner since they are shut down in the cluster. The VMware Tools service collects the operating system. PowerCLI won’t be able to get the operating system information if it isn’t accessible.

Using PowerCLI to Create CSV Reports

VMware Tools is an in-guest service that offers the hypervisor with extra information and administrative features for both Windows and Linux VMs. Most of the time, this will offer a clean shutdown, operating system information, and a higher-resolution console view of the virtual machines.

Pipe the preceding script into the Export-CSV cmdlet for a straightforward method to report on and deliver this information. Export-Csv will generate a CSV file with the same data as the console.

PS51> Get-VM | Sort-Object -Property Name | Get-View -Property @(“Name”, “Config.GuestFullName”, “Guest.GuestFullName”) | Select -Property Name, @{N=”Configured OS”;E={$_.Config.GuestFullName}}, @{N=”Running OS”;E={$_.Guest.GuestFullName}} | Export-CSV C:report.csv -NoTypeInformation

You should be able to access the CSV file in Excel to evaluate the report after executing the following code.

VM information in a CSV fileVM information in a CSV file

Using PowerCLI to inspect virtual hard disks

Another helpful command to be aware of is Get-Harddisk. You can investigate information about virtual hard drives associated to VMs with the Get-HardDisk cmdlet.

For example, you might use the following command to get information on the virtual hard drive associated to the exchange1 VMs:

PS51> Get-VM -Name exchange1 | Get-HardDisk | Format-List

query information about the virtual hard disk attached to the exchange1 VMsInformation about the virtual hard drive associated to the exchange1 VMs is being queried.

Some of this information, such as the capacity in KB vs. GB, may be redundant. However, understanding the StorageFormat (thin/thick provisioning kinds) is useful. Also, you’ll need to know the name of the VMDK file.

If you observe a similar problem and all VM hard drives are on the same datastore volume, for example, knowing this information might help you debug faster.

Using PowerCLI to inspect virtual network adapters

You should evaluate the virtual network adapters in addition to the hard disk information of your virtual machines. The Get-NetworkAdpter cmdlet may be used to examine these attributes for a single VM.

PS51> Get-NetworkAdapter -VM myVM

You were looking for all VMs on the same network in this port before, but this time you just want to view the adapters attached to a specific VM.

Get-NetworkAdapterGet-NetworkAdapter

When debugging VMs with several network adapters attached, this is handy. You can see whether those adapters are linked to the correct networks fast and easily.

Invoke-VMScript allows you to run PowerShell scripts on virtual machines.

You may also launch PowerShell code directly within the VM using the Invoke-VMScript command; no network connection is required. If you’ve ever used PowerShell Direct in a Hyper-V environment, you’ll be familiar with the process.

Instead of using the Invoke-Command cmdlet over the network or starting a PowerShell Remoting session, the Invoke-VMScript cmdlet may deliver instructions directly to the VM without the need for WinRM or SSH access.

For instance, suppose you wanted to do a basic directory listing on a VM named exchange1. As seen below, you would use dir C: as the value for the ScriptText argument.

PS51> Invoke-VMScript -VM exchange1 -ScriptText “dir C:”

The results are the same as if you had typed the instructions directly into the VM terminal. The output of the command that was performed on the VM is then relayed by Invoke-VMScript.

Invoke-VMScriptInvoke-VMScript

While this is a simple example, you may make it as complex as you like. Within the ScriptText argument of this PowerCLI cmdlet, you may choose batch, PowerShell, or Bash types.

You may also enhance your skills. Use Invoke-VMScript to execute PowerShell code with a distinct $script variable for the ScriptText argument input as seen below. This enables us to provide the VM with more personalized script input.

PS51> $script = ‘Get-Disk’ $guestCredential = Get-Credential Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential -ScriptType Powershell

The value of the ScriptText argument must be a string. This is why the single-outer-quotes are required in the $script variable.

You may have also seen the GuestCredential option being used. This option is used to log in to the virtual machine’s operating system. If you wish to execute the script as a different account, this argument is extremely beneficial.

When you run your script, you should get something like this.

Using Invoke-VMScript to run a script on a virtual machineUsing Invoke-VMScript to run a script on a virtual machine

The disk information for the VM is obtained as a consequence of this script. Because the VMware Virtual Disk is the friendly name of the disk, you should know it’s a remote VM.

Using Get-View in a More Advanced Way

Some of the basic cmdlets may have returned a property named ExtensionData. Many PowerCLI cmdlets may be piped to the PowerShell Get-Member cmdlet to discover this property. If you’ve ever wondered what it meant, now’s your chance to learn.

Understanding the Get-View cmdlet is the next stage in our journey. To give the nice and easy output of Get-VM, VMware PowerCLI uses a variety of queries to the VM. However, there is a lot hidden behind the scenes that can only be accessed using the Get-View cmdlet.

This cmdlet is likely to appear in a number of scripts. It’s a good idea to spend some time getting acclimated to seeing how this cmdlet works. Use the Gather-View cmdlet to get some virtual machine information to get started (exchange1 in this example).

As you can see, this cmdlet’s Filter options demand the usage of a PowerShell hashtable rather than individual string values. When creating your own scripts, keep this in mind!

PS51> Get-View -ViewType VirtualMachine -Filter @{“Name” = “myVMName”}

The command above returns a large amount of nested configuration information as well as method choices for performing operations against the VM or retrieving object attributes.

Get-ViewGet-View

You may analyze those nested options using dot notation if you use the same script above and send the result to a variable. You can view and obtain all of the extra information about the guest operating system that you would wish to report on using PowerCLI from here.

PS51> $VM = Get-View -ViewType VirtualMachine -Filter @{“Name” = “myVMName”} PS51> $VM.Guest

Using the Filter Parameter with Get-ViewUsing the Filter Parameter with Get-View

Maybe you’d want to focus on just one home. If that’s the case, you might try the following to narrow down a certain number.

PS51> $VM.Guest.GuestFullName

For all sorts of VMware objects, there are several Get-View alternatives. Feel free to investigate all of these possibilities, as well as this helpful blog from VMware that delves further into this powerful cmdlet!

Learn from your Clicks using VMware Code Capture

If you’d want to use PowerCLI but don’t want to write code by hand, vCenter’s Code Capture feature is for you. Code Capture is a new developer tool that works similarly to the Active Directory Administrative Center in terms of functionality. In this PowerCLI lesson, we’ll look at Code Capture.

This utility keeps track of everything you do in the GUI. It then translates all of those actions into PowerCLI scripts for you.

By default, Code Capture is not turned on. To turn it on, open up your vCenter appliance and navigate to  Menu –> Developer Center as shown below.

In vSphere, go to the Developer Center menu item.In vSphere, go to the Developer Center menu item.

Toggle the Enable Code Capture option once you’re in the Developer Center tab.

Activating code captureActivating code capture

When you activate Code Capture, a red Record button will appear in the vCenter header. Once enabled, you must record the GUI activity by pressing the record button anytime you want an action captured and converted to PowerCLI output.

Indicator that code capture is enabledIndicator that code capture is enabled

You may now record at any time by clicking the red record icon next to your logged in user area of vCenter.

Go through the instructions to create a new virtual machine to demonstrate Code Capture.

  1. To start recording, click the Record button. The red Record button will start pulsating as a result of this.
  2. Create a New Virtual Machine by right-clicking your VMware host. Follow the instructions to create a new virtual machine with all default settings.
  3. Once the virtual machine has been built, click the record button to begin recording and then stop.

It’s possible that the output will be little more verbose than expected. All activities taken when exploring the GUI prior to generating the VM are included in the output. However, there should be a section in the comments beginning with CreateVM Task. This is where the code for creating a virtual machine starts.

The CreateVM Task output of the New VM Wizard procedure in the GUI using Code Capture is shown below:

Code was created by the Developer Center.Code was created by the Developer Center.

This output is a bit overwhelming at first, but it exposes you to the vast array of virtual machine setup choices. You may now make changes to your own scripts and personalize them to your heart’s content, depending on your requirements.

The Code Capture tool isn’t simply for creating virtual machines. It can also generate code for networking adjustments, minor VM tweaks, and host configuration changes. You can cut down on the time it takes to install infrastructure as code by reviewing the PowerCLI output of GUI activities.

Summary of the PowerCLI Tutorial

You covered a lot of territory in this essay. You did an excellent job! I hope you’ve seen the value that PowerCLI adds to VMware infrastructure management.

PowerCLI has a number of cmdlets for a variety of product bases, however we just explored a handful of them today. Keep an eye on our site for additional posts about this fantastic tool!

Additional Reading

The “vmware powercli download” is a command-line tool for managing VMware environments. It allows users to easily create, edit and delete virtual machines, configure vSphere hosts, manage the ESXi host cluster and more.

Frequently Asked Questions

Related Tags

  • powercli get-vm details
  • how to run powercli
  • vmware powercli commands cheat sheet
  • install powercli
  • vmware powercli commands list

Table of Content