Manage Learn to apply best practices and optimize your operations.
This article is part of our Essential Guide: Enable VMware automation with these top tools and techniques

Execute PowerCLI commands to manage VMware infrastructure

PowerCLI cmdlets like Get-VM can be built out to obtain information about VMs in a VMware infrastructure, including those with attached CD-ROMs and those with snapshots.

Virtualization administrators can use PowerCLI commands to ease management tasks in VMware infrastructures. All...

you have to do is install PowerCLI and ensure the commands have been auto-loaded.

To connect to vCenter, open the PowerCLI client and issue the following command:

connect-viserver "my_vcenter_server"

Next, log in using your vCenter credentials. You can explore some useful PowerCLI cmdlets.

Start with the Get-VM command

The Get-VM command is very useful by itself, but you can also chain the command with additional PowerCLI commands from left to right to select and extract the data you want. To get the names of all the VMs and their statuses, first execute the following:

get-vm

As you can see in Figure A below, you'll get back a comprehensive list of VMs, their power status and their memory sizing to boot. 

Get VM status
Figure A. One easy command to get VM status

Add to the Get-VM command

Expanding on that, pipeline the command below to get more information:

get-vm | select * | ft

If you're looking for specific servers, the Get-VM command can be changed using a wild card. For example, if there is a naming convention at play -- i.e., web servers -- it could be as simple as:

get-vm web*

You can refine the data further by using the pipeline |. Also, use the select statement to select various attributes to display. The data is then passed into a table format by using ft. Without the table addition, it can get rather messy.

get-vm | select * | ft

Lastly, to export the data to a file -- for Excel manipulation and such -- you can append ' | export-csv c:\myfile.csv'. At this point, it becomes a simple CSV import.

get-vm | select * | export-csv 'C:\Users\Stuart\Desktop\vmlist.csv' -NoTypeInformation

The NoTypeInformation specification just excludes some extraneous typing.

To sort by the server power state, use the sort item in the pipe.

get-vm | select * | sort Powerstate | Export-Csv 'C:\Users\Stuart\Desktop\test4.csv' -NoTypeInformation.

The PowerCLI cmdlets can be easily modified to show only the machines that are powered off.

Access additional VM information

By default, the select command shows only a limited subset -- i.e., those bits that the administrator needs most of the time. There are many additional items that can be accessed.

A good example is locating all those VMs with attached CD-ROMs. When a CD-ROM is attached, the VM can't migrate between hosts.

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name

Although these PowerCLI commands look complex, you're just using the pipeline to get a list of the VMs that have a ConnectionState.Connected equal to true -- i.e., a CD-ROM attached.

Get VMs with CDs
Figure B. Get all VMs with attached CDs

To export this to a file, it's as easy as adding another pipe, essentially repeating the export-csv component with an alternate name. The same can be done for all the examples in this article.

Other useful items include the ability to search for those ISO and OVA files that people leave strewn in the data store. Luckily, the humble dir command still exists.

dir vmstores:\ -Recurse -Include *iso *ovf | ft -a

Snapshots are also frequently left behind, which can degrade performance and, in worst case scenarios, fill up the data store. It can even cause VMs with thin disk formatting to stop working.

Unfortunately, there's no easy way to locate VMs with snapshots using the GUI. To get this information, use the following PowerCLI commands:

get-vm | get-snapshot | select VM, Name, SizeGB, Created

Snapshots with size
Figure C. List of snapshots with size

The commands will work, but the size will look a bit odd, with way too many decimal points. To fix this issue, you can do a bit of inline math to round it correctly using inbuilt functions.

get-vm | Get-Snapshot | select VM, Name, @{ n="SpaceUsedGB"; e={[math]::round( $_.SizeGB, 2)}}, created

A quick and easy summary of the inline math can be found on the VMware community site.

Learn different command options

With all these different PowerCLI cmdlets, you might be wondering what details and options are available to you. To see what's available for the Get-Snapshot command, select an item and press the tab key. Repeating this action will cycle through all the possible options.

By the same token, there's an extensive help functionality that's built into the PowerCLI client. Simply use the Get-Help option. The Get-VM command is used in the example below, but any command can be substituted as needed.

get-help get-vm -examples

These PowerCLI commands are primarily used to give some insight into quick two-minute scripts. The next step I recommend is looking for a more fully featured daily/weekly check script to report the health of the infrastructure.

This was last published in January 2018

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

What other PowerCLI cmdlets are useful to VMware administrators?
Cancel

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close