Monday 27 October 2014

Turning out the lights- powering down your VM's overnight

In the last post I showed how to use the PowerShell module for CaaS to perform some basic operations.
A common benefit of cloud is touted as "you can save $ by turning off your servers when you're not using them".
So, to demonstrate how that can be done here with a quick recipe.
I'm assuming you only want to do this on your Development environments and not on production, so firstly, pick the network where your lab/testing servers are stored. I have a network called "Ant Dev" with my test servers inside. I don't need them to be running over the weekend.
$devNetwork = Get-CaasNetworks | Where name -like "Ant Dev"
Now, get all the servers in that network by searching for servers in that network Id.
$devServers = Get-CaasDeployedServer | Where networkId -eq $devNetwork.Id
 foreach ( $server in $devServers ) {
  Write-Host Shutting down $server.name 
  Set-CaasServerState -Action Shutdown -Server $server
}

Now, wrap this script up into a ps1 file and store somewhere you can call it from Scheduled tasks. I've used c:\Users\Anthony\Documents\caas-sleep.ps1 for this example
Copy the code below into ISE and set the user and file location to meet your requirements
$action = New-ScheduledTaskAction –Execute "powershell.exe" -Argument "c:\Users\Anthony\Documents\caas-sleep.ps1" -AsJob
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Friday -At 7pm
$user = "MyServer\Administrator"
$S = New-ScheduledTaskSettingsSet
$D = New-ScheduledTask -Action $action -Principal $user -Trigger $trigger -Settings $S
Register-ScheduledTask T1 -InputObject $D
This will create a Scheduled task in Windows to run the job at 6pm every Friday. Customise the script above to set the 'wake-up' version for late Sunday night. Set-CaasServerState takes -Action [PowerOff | PowerOn | Restart | Shutdown] PowerOff and PowerOn are the hard-off options and restart and shutdown use VMware tools to initiate a clean shutdown or reboot.

No comments:

Post a Comment