5 PowerCLI Cmdlets that every Administrator should know!

It’s no secret that PowerCLI is awesome. I’m always trying to pick up new things and it helps you streamline a lot of management tasks in any sized environment. People always ask me if you should learn PowerCLI in a small environment and the answer is yes. It’s not just for the really massive environments, although they certainly have the most to gain from using it! Generally, if you’re learning and getting a grasp on the vSphere product suite, learn the GUI to start but then when you’ve mastered that, spend some time in the CLI!

Luke has a great post over here which outlines the getting started process so I won’t be covering that. I just figured I would throw out a “Top 5” post with some of my favorites. These are my own personal favorites to recommend to somebody who asks where to start and get their feet wet with this stuff. If you have some of your own to recommend, feel free to throw them out in the comments section!

I’d also highly recommend starting out in the vSphere Documentation Center. It’s not really what I would consider bedtime reading but you can find some excellent new cmdlets and see usage. Another great reference is the VMware vSphere PowerCLI Reference: Automating vSphere Administration book. That book has some great sample scripts and if you’re anything like me, it’s good to see a sample that you can modify to make them work the way you want.

1. Get-VM

It’s a classic. It has been around for awhile and we have to start with this one. Good old Get-VM! It is a great place to start. It can produce a simple inventory of all VMs that are currently found within vCenter . Using the Get-VM cmdlet, and the standard PowerShell Select-Object cmdlet, you can retrieve the particular properties that are of interest to you and use Export-CSV to export that data into a CSV file for further data manipulation.

Get-VM | Select-Object Name,NumCPU,MemoryMB,PowerState,Host |
Export-CSV VMs.csv -NoTypeInformation

You can also use Get-VM to find snapshots you might have forgotten about in your environment.

Get-VM | Get-Snapshot | format-list | out-file c:\snapshots.txt

2. Move-VM

If you’re looking to move all the VMs from one host to another for possible maintenance, you can use the Move-VM cmdlet. You can also use Maintenance Mode as well, but we’re focusing on the CLI here! Below we are taking VMs on esxi01 and moving them to esxi02.

Get-VMHost esxi01 | Get-VM | Move-VM -Destination (Get-VMHost esxi02)

Maybe you’re only interested in live migrating a single VM? This command will take a VM called MyVM and move it to the esxi01 host.

Move-VM -VM 'MyVM' -Destination 'esxi01'

With the command below, you can also do a storage vMotion. MyVM is the VM name and MyDatastore is your destination datastore.

Move-VM -VM 'MyVM' -Datastore 'MyDatastore'

3. New-VM

If you’re looking to build a single VM, you can use New-VM using your information to create a single VM. This basically builds a blank VM, you’ll have to deploy the OS, etc. You can see below for other options to deploy multiple VMs and use templates and customization specifications instead.

New-VM –Name “MyVM” –VMHost esxi01 –ResourcePool Production –DiskGB 20 –DiskStorageFormat Thin –DataStore datastore1 –MemoryGB 3 –NetworkName “ProdNetwork”

This is one I have used a lot in testing environments where you are cloning machines all the time. What this does below is creates a new machine called NewVM8 from a clone of OriginalVM. It will deploy your VM to the esxi01 host, although the ResourcePool parameter accepts ResourcePool, Cluster, vApp, and standalone VMHost objects so you can use any of those.

$myResourcePool = Get-ResourcePool -Name esxi01
$mySpecification = Get-OSCustomizationSpec -Name WindowsSpec
New-VM -VM OriginalVM -Name NewVM8 -OSCustomizationSpec $mySpecification -ResourcePool $myResourcePool

If you’re not wanting to run a script for each VM, it is possible to create multiple VMs from a CSV. This is a relatively basic way to get the job done. For multiple VMs, create a CSV like this.

multiVM

Then after you have the CSV, you can use the New-VM cmdlet below and deploy multiple VMs. For this I typically don’t go crazy as it taxes vCenter if you have a ton of VMs. If you think this is neat, check out the post Andy wrote here. It’s a seriously awesome script and will take it even further.

$vms = Import-CSV C:\Scripts\NewVMs.csv

foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization `
-Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync
}

4. Invoke-VMScript

How many times have you logged into a VM to run a script inside the guest operating system, only to logout again a few minutes later? This Invoke-VMScript cmdlet will save you the hassle of doing that.

The script below will run a BAT script. In BAT scripts, to access environment variables, you must use the following syntax: %<environment variable>% (for example, %programfiles%).

The outer quotes ($script = ‘…’) are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.

$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "%tmp%\inforeport"'

Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential -ScriptType Bat

5. Get-VMHost

A common issue in virtual environments is NTP. Hosts are out of sync, then the login issue creep up. Performance monitoring quits working, etc. It’s overlooked a lot. You can use Host Profiles if you’ve got Enterprise Plus licensing, but you can also use the command below to modify and change the NTP settings on a host.

Get-VMHost esx01 | Add-VMHostNtpServer -NtpServer ntpservername

Another good reason to use Get-VMHost would be to easily find what host a VM is currently running on.

$MyVM = Get-VM -Name MyVM
Get-VMHost -VM $MyVM

Wrap up:

To wrap up, these are just a few of the awesome cmdlets you can use, but these 5 are ones that every single VMware administrator should know and commit to your brain. VMware is always adding new features but these are a few you should commit to memory. If you’re a fan of other ones post them in the comment section and let us know how you use them.

[the_ad id=”4738″][the_ad id=”4796″]

Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

13 thoughts on "5 PowerCLI Cmdlets that every Administrator should know!"

Leave a comment

Your email address will not be published. Required fields are marked *