Created
September 21, 2016 03:27
-
-
Save mrbodean/f99b4dc7528c57ef81796d04bad9733e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Garth Jones | |
# http://www.enhansoft.com | |
# | |
# Created Date: 2016-08-01 | |
# Version: 1.00 | |
# | |
# Thanks Sean Kearney @energizedtech for the start point. | |
# | |
################################################################## | |
# User Defined Variables | |
################################################################## | |
Clear-Host | |
$CSVPath='C:\temp\' | |
$CSVName ='computerlist.csv' | |
$Downtime = 5 | |
$Uptime = 60 | |
# Test for the CSV file | |
if (test-path -Path $CSVPath$CSVName) | |
{ | |
$Start=$True | |
write-host "Starting VM" | |
} | |
else | |
{ | |
$Start=$False | |
write-host "Stopping VM" | |
} | |
# Starting the VMs | |
If ($Start) | |
{ | |
$vmlist = Import-CSV -Path $CSVPath$CSVName | |
Foreach ($VM in $VMlist) | |
{ | |
write-host "Starting VM: $VM.VMname" | |
Start-VM -Name $VM.VMname | |
write-host "Sleeping $Uptime seconds" | |
start-sleep $Uptime | |
} | |
Remove-Item $CSVPath$CSVName | |
write-host "Deleting: $CSVPath$CSVName" | |
} | |
else | |
{ | |
# Staopping the VMs | |
New-Item -Path $CSVPath -Name $CSVName -ItemType File -Force | |
Add-Content -path $CSVPath$CSVName -value VMname | |
GET-VM | where {$_.state -eq ‘running’} | Select-Object -expandproperty Name | foreach { Add-Content -Path $CSVPath$CSVName -value $_} | |
$vmlist = Import-CSV -Path $CSVPath$CSVName | |
Foreach ($VM in $VMlist) | |
{ | |
write-host "Stopping VM: $VM.VMname" | |
Stop-VM -Name $VM.VMname -force | |
write-host "Sleeping $Downtime seconds" | |
start-sleep $Downtime | |
} | |
write-host "Rebooting Server" | |
# Reboot the Server | |
# Uncomment the next line to reboot the server | |
# Restart-Computer -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Having a Priority property on the VM would help. This should be a unique value.A VM with a Priority of 1 would start 1st and shut down last. A VM with a priority on 1000 would shut down 1st and start up last.
You could mock this feature up with a config file to hold the priority until the feature is added to Hyper-V. The script could read the config file and look up the vm and add the priority to the computername.csv that would allow you to sort on the priority for the startup and shutdown actions,