Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kevin-Bronsdijk/0a0aa4670e1e0431d7ab to your computer and use it in GitHub Desktop.
Save Kevin-Bronsdijk/0a0aa4670e1e0431d7ab to your computer and use it in GitHub Desktop.
automatically-deallocate-azure-vms
#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