Skip to content

Instantly share code, notes, and snippets.

@benmccallum
Created January 31, 2016 23:21
Show Gist options
  • Save benmccallum/f7ec1fb9c32a217035e7 to your computer and use it in GitHub Desktop.
Save benmccallum/f7ec1fb9c32a217035e7 to your computer and use it in GitHub Desktop.
Automate VM shutdown/startup with timezone offset
$TimezoneOffset = -6
$currentTime = (Get-Date).ToUniversalTime()
Write-Output "Runbook started"
Write-Output "Current UTC/GMT time [$($currentTime.ToString("dddd, yyyy MMM dd HH:mm:ss"))] will be checked against schedules"
Write-Output "A TimeZone offset of [$TimezoneOffset] will be used in comparisons"
$TimeRange = "29 January"
# Initialize variables
$rangeStart, $rangeEnd, $parsedDay = $null
$currentTime = (Get-Date).ToUniversalTime()
$midnight = $currentTime.AddDays(1).Date
try
{
# Parse as range if contains '->'
if($TimeRange -like "*->*")
{
$timeRangeComponents = $TimeRange -split "->" | foreach {$_.Trim()}
if($timeRangeComponents.Count -eq 2)
{
$rangeStart = (Get-Date $timeRangeComponents[0]).AddHours(-1 * $TimezoneOffset)
$rangeEnd = (Get-Date $timeRangeComponents[1]).AddHours(-1 * $TimezoneOffset)
# Check for crossing midnight
if($rangeStart -gt $rangeEnd)
{
# If current time is between the start of range and midnight tonight, interpret start time as earlier today and end time as tomorrow
if($currentTime -ge $rangeStart -and $currentTime -lt $midnight)
{
$rangeEnd = $rangeEnd.AddDays(1)
}
# Otherwise interpret start time as yesterday and end time as today
else
{
$rangeStart = $rangeStart.AddDays(-1)
}
}
}
else
{
Write-Error "`tWARNING: Invalid time range format. Expects valid .Net DateTime-formatted start time and end time separated by '->'"
}
}
# Otherwise attempt to parse as a full day entry, e.g. 'Monday' or 'December 25'
else
{
# If specified as day of week, check if today
if([System.DayOfWeek].GetEnumValues() -contains $TimeRange)
{
$todayOffset = (Get-Date).ToUniversalTime().AddHours($TimezoneOffset)
Write-Output "Today offset: $todayOffset"
if($TimeRange -eq $todayOffset.DayOfWeek)
{
$rangeStart = $todayOffset.Date.AddHours(-1 * $TimezoneOffset)
}
else
{
# Skip detected day of week that isn't today
}
}
# Otherwise attempt to parse as a date, e.g. 'December 25'
else
{
$rangeStart = (Get-Date $TimeRange).ToUniversalTime().AddHours(-1 * $TimezoneOffset)
}
if($rangeStart -ne $null)
{
$rangeEnd = $rangeStart.AddHours(23).AddMinutes(59).AddSeconds(59) # End of the same day
}
Write-Output $rangeStart
Write-Output $rangeEnd
}
}
catch
{
# Record any errors and return false by default
Write-Error "`tWARNING: Exception encountered while parsing time range. Details: $($_.Exception.Message). Check the syntax of entry, e.g. '<StartTime> -> <EndTime>', or days/dates like 'Sunday' and 'December 25'"
return $false
}
# Check if current time falls within range
if($currentTime -ge $rangeStart -and $currentTime -le $rangeEnd)
{
return $true
}
else
{
return $false
}
Write-Output "Runbook completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment