Maksim Kabakou - Fotolia
Use these 5 PowerCLI cmdlets to automate vCenter tasks
With just a handful of cmdlets -- Get-VM, Set-VM, Start-VM, Stop-VM and Stop-VMGuest -- you can use PowerCLI to automate vCenter tasks at scale, something you can't do with a GUI.
With the shift toward prioritizing data center automation, many IT administrators no longer use the traditional GUI to manage large and complex systems such as vCenter. Instead, you can use a command-line interface and scripting language to automate tasks at scale.
VMware publicly recommends its PowerShell-based tool PowerCLI for automating tasks in vSphere. You can do just about anything in PowerCLI that you can with the vSphere GUI, but PowerCLI can manage VMware VMs more quickly and efficiently through vCenter.
If you're familiar with PowerShell, you can transition smoothly to managing vCenter tasks -- and consequently, VMs -- through PowerCLI. You can easily create PowerCLI scripts to automate many complex tasks that you can't do with a GUI. For instance, you can change the CPU and memory for hundreds of VMs using a single PowerCLI cmdlet. A GUI would take significantly longer to complete the same task.
To get started using PowerCLI for managing vCenter tasks, review these 5 cmdlets: Get-VM, Set-VM, Start-VM, Stop-VM and Stop-VMGuest.
Get-VM
Use the Get-VM PowerCLI cmdlet to obtain information about VMs. Get-VM can show you a lot of information, including a VM's name, power state, guest OS, hardware info, ESXi host, VM hardware version and notes.
Use Select-Object to show only the name, VM host, guest and power state a VM:
C:\> Get-VM Test-1 | Select-Object Name,VMHost,Guest,PowerState | Format-List Name : Test-1 VMHost : Vmhost-1 Guest : Test-VM:Microsoft Windows Server 2016 (64-bit) PowerState : PoweredOn
You can combine Get-VM with other PowerCLI cmdlets for further information. For example, if you want to see all VMs in a specific cluster that have a Windows OS, pipe the Get-Cluster cmdlet to Get-VM:
C:\> Get-Cluster -Name 'My Cluster' | Get-VM | Where {$_.guest -like "*Windows*"} | Select Name Name ---- Test-1 Test-2 Test-3
Set-VM
The Set-VM cmdlet enables you to modify an existing VM in vCenter. Like Get-VM, it deals with many VM settings, but you can use it to change these settings rather than simply display them.
For example, you can set the number of CPUs to two and the memory to 8 GB for a VM. Specify the –Confirm parameter to bypass the confirmation message:
C:\> Set-VM Test-1 -NumCpu 2 -MemoryGB 8 -Confirm:$False Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Test-1 PoweredOff 2 8.000
Set-VM also enables you to restore to specific snapshots taken in vCenter:
C:\> Set-VM Test-1 -Snapshot (Get-Snapshot -VM Test-1 -Name 'testsnapshot') -Confirm:$False Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Test-1 PoweredOff 2 8.000
Start-VM
The Start-VM PowerCLI cmdlet does exactly what it says: It starts a VM. This cmdlet only has a couple parameters: the name of a VM and whether to run that VM asynchronously. You can use Start-VM to boot many VMs at once.
For instance, if you must boot several VMs that all start with the same naming, such as Web-*, you can use the following method:
C:\> Start-VM -VM Web-* -RunAsync Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Web-Server1 PoweredOn 2 8.000 Web-Server2 PoweredOn 2 8.000 Web-Server3 PoweredOn 2 8.000 Web-Server4 PoweredOn 2 8.000
When using the –RunAsync parameter, all specified VMs boot without PowerCLI waiting for a specific action to take place, which significantly speeds up the task.
Stop-VM and Stop-VMGuest
You can use either Stop-VM or Stop-VMGuest to turn off a VM, but these cmdlets work differently.
Think of Stop-VM as pressing the power button on a server until it turns off. Though simple, the VM doesn't shut down gracefully, and using this cmdlet might cause issues.
Stop-VMGuest, on the other hand, communicates with VM tools on the OS to perform a more graceful shutdown, similar to performing a shutdown through Windows. For Stop-VMGuest to work, you must have already installed and run VMware Tools on the VM.
Here's an example of Stop-VM:
C:\> Stop-VM -VM Test-1 -Verbose -Confirm:$False VERBOSE: 4/15/2019 2:22:45 PM Stop-VM Started execution VERBOSE: Performing the operation "Stop-VM" on target "VM 'Test-1'". VERBOSE: 4/15/2019 2:22:45 PM Stop-VM Finished execution Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Test-1 PoweredOff 2 8.000
Try Stop-VMGuest for this result:
C:\> Stop-VMGuest -VM Test-1 -Verbose -Confirm:$False VERBOSE: 4/15/2019 2:23:35 PM Stop-VMGuest Started execution VERBOSE: Performing operation "Shutdown VM guest." on VM "Test-1". State IPAddress OSFullName ----- --------- ---------- Running {172.16.5.250} VERBOSE: 4/15/2019 2:23:35 PM Stop-VMGuest Finished execution
Note that Stop-VMGuest doesn't wait for the OS to finish shutting down, so it displays the output right away.