Quick tip: Handy Scripts for Local Hyper-V Management
01 Jan 2013 Leave a comment
Here is a quick post with a small script that I find incredibly handy for managing my local Hyper-V machines (on windows 8).
It simply ensures what set of VMs are running at a given time – attach that to a shortcut on the desktop and it saves me 3 minutes every day; I find it useful.
Why? Quite often, I need to switch from one set of VMs to another for different tasks (VMWare term is “teams”) e.g. switching between SP2010 and SP2013 development. Sometimes just turn them off if I need the resources for something else.
Normally you just go into Hyper-V manager and start/stop the relevant VMs. Automating that was quite simple and painless – the download is here.
- I have one PowerShell script (SetRunningVMs.ps1) that handles the VM management and then a couple of batch files for executing that script with proper parameters. The script is:
<# .SYNOPSIS This is a simple Powershell script that adjust what VMs are running on the local Hyper-V host. .DESCRIPTION This script will start/resume the requested VMs and suspend all other VMs to give you maximum power and make it easy to switch from one development task to another, i.e. switch teams. .EXAMPLE Make sure that only the dev1 and ad01 machines are running: ./SetRunningVMs.ps1 'dev1' 'ad01' Stop all VMs (no arguments) ./SetRunningVMs.ps1 .NOTES Requires admin rights to run. Start using admin shell or use one of the provided batch files. Pauses on error. .LINK http://soerennielsen.wordpress.com #> param( [Parameter(ValueFromRemainingArguments=$true)][string[]] $allowedVMs = @() ) try{ get-vm |? { $_.State -eq "Running" -and $allowedVMs -notcontains $_.Name } |% { Write "Saving $($_.Name)"; Save-VM $_.Name } get-vm |? { $_.State -ne "Running" -and $allowedVMs -contains $_.Name } |% { Write "Starting $($_.Name)"; Start-VM $_.Name } write "SetRunningVMs succesfully done" } catch{ Write-Error "Oops, error:" $_ pause }I assume that the PowerShell Hyper-V module is loaded; it has always been the case in my tests.
-
I have a number of batch files for easily executing the script, one for turning off (suspend) all VMs, one for a SP2010 team and one for the SP2013 team. It’s a one-liner bat file that will work with both PowerShell 2 and 3, with or without UAC.
To Start my SP2010 team (StartSP2010Team.bat):
powershell -noprofile -command “&{ start-process powershell -ArgumentList ‘-noprofile -file %~dp0SetRunningVMs.ps1 \”AD01\” \”Dev1\”‘ -verb RunAs}”
(where you replace the bold VM names above with your own)
To start my SP2013 team (StartSP2013Team.bat)
powershell -noprofile -command “&{ start-process powershell -ArgumentList ‘-noprofile -file %~dp0SetRunningVMs.ps1 \”AD02\” \”SP2013\”‘ -verb RunAs}”
To stop all VMs
powershell -noprofile -command “&{ start-process powershell -ArgumentList ‘-noprofile -file %~dp0\SetRunningVMs.ps1′ -verb RunAs}”
If you have UAC enabled (as I do) you will be prompted, otherwise it will just suspend/resume the relevant VMs.
It took about two hours to write the scripts, where the hardest part was getting the batch files properly in shape with escape characters and UAC.
You gotta love PowerShell




That’s it for the guests!







