Created
July 28, 2015 15:38
-
-
Save Kevin-Bronsdijk/0a0aa4670e1e0431d7ab to your computer and use it in GitHub Desktop.
automatically-deallocate-azure-vms
This file contains hidden or 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
#Change VMName, Service Name and min/max values in hours | |
$ExpectedUptime = 6 | |
$ForceShutdownAfter = 8 | |
$VMName = "name of VM" | |
$ServiceName = "Name of service" | |
$Now = Get-Date | |
$Wmi = Get-WmiObject -ComputerName $Env:COMPUTERNAME -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem" | |
$LastReboot = $Wmi.ConvertToDateTime($Wmi.LastBootUpTime) | |
$Uptime = $Now - $LastReboot | |
if ($Uptime.Hours -gt $ExpectedUptimeInHours -and $Uptime.Hours -lt $ForceShutdownAfter) | |
{ | |
#"Send alert via email..." | |
} | |
if ($Uptime.Hours -eq $ForceShutdownAfter -or $Uptime.Hours -gt $ForceShutdownAfter) | |
{ | |
#Force Azure VM shutdown | |
Stop-AzureVM -Name $VMName -ServiceName $ServiceName -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment