Hyper-V On-the-Go: Lab VM State Change

Shutting down or powering up (or even rebooting) the lab en-masse via Hyper-V, nuts to that!

Do it the PowerShell way. No muss, no fuss and way quicker.

function Set-LabVMState
{
    # Power On/Off Lab
    Param
    (
        [ValidateSet('Start','Stop','Restart','Resume','Suspend')]
        $vmOption,
        $txtMatch = '^((ad|app)\.|incep)'
    )
    $vmAction = $vmOption + '-VM'
    $rpsVMs = Get-VM | ?{ $_.Name -match $txtMatch }
    $rpsVMs | & $vmAction -Verbose
}

Note the $txtMatch variable is defaulted with the match string '^((ad|app)\.|incep)'. This regex looks for any VM Name beginning with either AD.APP. or Incep (short for inception).

Load the function into your profile so it loads anytime to run PowerShell.

Shut it down, Mon! Shut it all down!!

Need to shut the lab down?

No problem! Just run this this snippet:

Set-LabVMState -vmOption Stop

and if all goes well, you should see the verbose remarks:

and the VMs should now be powered down.

Two errors you may see:

  1. Domain Controllers may not shutdown. These usually want you to shutdown from within the OS. The function error returned would look like this:
PowerOff_Err.png

and the VM would not shutdown:

and would require a manual shutdown in the OS or turn off the VM manually:

2. Running the function against a VM already powered off would return a warning like this:

Turn it o-o-o-on, turn it on, turn it on again!

Firing the lab up is equally as easy, just run this snippet:

Set-RpsVMState -vmOption Start

And again if you attempt to start a VM this is already on:

And the lab is online once more:


Warpin’ on Down the Highway

Profile snippets and functions are another handy tool in our portable labs toolbox. They make quick work for some of the more onerous tasks whilst working with your VMs. But since different situations demand different solutions far too many to imagine here, I will just have to cover them in other blogs. Until then “Lab long and prosper!“.

More Blogs in the ‘Boldly Going” Series

  1. Hardware Considerations for Portable Labs
  2. Enabling the Hyper-V Engine
  3. Reclaiming Drivespace
  4. Sending Files to Your VMs (Part 1)
  5. Sending Files to Your VMs (Part 2)
  6. Sending Files to Your VMs (Part 3)
  7. Changing the Lab VM State

Authors