Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Change VM names to your own names
$baseVMs = @("ROUTER","AD")
$sqlVMs = @("SPSQL2016")
$sharepointVMs = @("SP2016-1","SP2016-2")
# -------------------------------------------------------------------------------------------------
function Start-VMSet($vmSet){
$vmStarted = $false
$waitTime = 10
foreach ($vmName in $vmSet) {
$vm = Get-VM -Name $vmName
write-host $vm.Name ": " -NoNewline
switch ($vm.State) {
"Running" { write-host "already running" -f Green }
"Off" {
write-host "starting" -f Cyan
Start-VM -VM $vm
$vmStarted = $true
}
default { write-host "unknown state:" $vm.State -f Yellow }
}
}
if ($vmStarted) {
write-host "Waiting for" $waitTime "seconds for machines to boot up..."
Start-Sleep -Seconds $waitTime
}
}
function Stop-VMSet($vmSet){
foreach ($vmName in $vmSet) {
$vm = Get-VM -Name $vmName
write-host $vm.Name ": " -NoNewline
switch ($vm.State) {
"Off" { write-host "already stopped" -f Green }
"Running" {
write-host "stopping" -f Cyan
Stop-VM -VM $vm
}
default { write-host "unknown state:" $vm.State -f Yellow }
}
}
}
write-host "1....: Start VMs`n2....: Stop VMs"
$command = Read-Host "Well?"
if ($command -eq 2 -and (Get-VM | ? { $_.State -eq "Running" }).Count -eq 0) {
write-host "Funny :) - There is no VM running. Did you mean start?" -f Yellow
exit
}
switch ($command) {
1 { # Start
write-host "Starting base VMs..." -f Magenta
Start-VMSet $baseVMs
write-host "Starting SQL VMs..." -f Magenta
Start-VMSet $sqlVMs
write-host "Starting SharePoint VMs..." -f Magenta
Start-VMSet $sharepointVMs
}
2 { # Stop
$withBaseVMs = Read-Host "Also stop base VMs? [y/N]"
write-host "Stopping SharePoint VMs..." -f Magenta
Stop-VMSet $sharepointVMs
write-host "Stopping SQL VMs..." -f Magenta
Stop-VMSet $sqlVMs
if ($withBaseVMs.ToLower() -eq "y") {
write-host "Stopping base VMs..." -f Magenta
Stop-VMSet $baseVMs
} else {
write-host "Skipped base VMs" -f Magenta
}
}
default { exit }
}
write-host "finished" -f Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.