SCVMM: Starting VMs with PowerShell
Well I promised that I’d post again soon, and I think 2.5 hours later fullfills that promise! Now that I have the basics down, I looked at creating the most obvious script - “Start all VMs that are stopped”. I did a lot of googling and couldn’t find any examples, so I worked it out myself - not that it is very complex at all. Here are the 2 lines of code that will start all stopped VMs:
$StoppedVMs = Get-VM -server localhost | where{$_.VMState -eq 'PowerOff'}
$StartResult = $StoppedVMs | Start-VM
It’s very straight forward; the first line creates a collection of VMs that are stopped. The second line starts each VM based on that collection. I used $StartResult simply to suppress the output created by the Start-VM cmdlet. The next script that I am planning to create will stop a VM, create a checkpoint and then start it again.