PowerShell Friday: Starting VMs
With PowerCLI you can manage and automate your VMware infrastructure from the command line. After you connect to vCenter you can do all kinds of nifty stuff. For example it is very easy to start virtual machines. You can use the complete name of the VM, or you can use a wildcard or use the cmdlet in a pipe. The cmdlet for starting VMs itself is easy to remember:
There are couple of ways you can use Start-VM. You can use it with the name of the VM
Start-VM -VM ThisVM
or from a piped input
Get-VM -Name ThisVM | Start-VM
Which one you use depends on what you want to achieve and your preference.
You can create pretty complex oneliners for starting VMs.
Get-VM -Name ThisV[MR] | Stop-VM | set-VM -numCPU 4 | Start-VM
Starting VMs with Wildcards
To select the right VM you might want to use wildcards. The table below shows your options.
|*||Matches zero or more characters, starting at the specified position||VM*||VMab, VM-123|
|?||Matches any character at the specified position||VM-?||VM-1, VM-a, VM-b|
|[ ]||Matches a range of characters||VM-[1-3]||VM-1, VM-2, VM-3|
|[ ]||Matches the specified characters||VM-[abc]||VM-a, VM-b, VM-c|
The examples are uppercase, but the cmdlets don’t differentiate between uppercase and lowercase.
Synchronous vs Asynchronous
If you have a lot of VMs you want to start it can take a while, because the objects in a cmdlet are handled one after the other (synchronous). If objects don’t depend on each other you can execute them simultaneously (asynchronous).
Start-VM -VM MyVM-00* -RunAsync
Do remember though that when you start a lot of VMs at the same time you might be hammering your storage system, so always make sure that you are not starting too many VMs or that your storage system can handle it.