#Google's time variable is based on the start date/time of 1/1/1970, universal time zone $startdatetime = [datetime]"1/1/1970 00:00:00Z" #getting local time and converting to universal time zone $currentdatetime = (get-date).touniversaltime() #Google time variable takes the 1/1/1970 date and wants the total seconds from that date to current time [int]$totalseconds = ($currentdatetime - $startdatetime).totalseconds #get a server key by signing up for the Google Maps Time Zone API #read more here #note: location just has to be somewhere in your area/time zone $timezone = invoke-restmethod ("https://maps.googleapis.com/maps/api/timezone/json?location=46,-122" + "&Timestamp=" + $totalseconds + "&key=") $timezonename = $timezone.timezonename #since I live in Washington, I have the two possible time zones. Obviously you'll want to add/change time zones based on your location(s). if ($timezonename -eq "Pacific Daylight Time"){$TZDiff = -7} if ($timezonename -eq "Pacific Standard Time"){$TZDiff = -8} #Now I'm pulling local timezone information $tz = ([timezoneinfo]::local).id write-host "Local Timezone setting: $tz" #creating a variable in case the timezone is incorrect #The ref'd timezone.ps1 script is from Peter Henchley's blog $changetime = "e:\scripts\timezone.ps1 $timezonename" if ($tz -ne $timezonename){powershell -executionpolicy bypass $changetime;write-host "Changed Timezone" -f yellow} #Once the time zone has been adjusted, now we check for time #We pull current time from www.timeapi.org, convert it to universal time, and add our time zone difference so it equals our current time zone $webtime = (([datetime](iwr http://www.timeapi.org/utc/now -usebasicparsing).content).touniversaltime()).addhours($TZDiff) $localhour = (get-date).hour if ($localhour -ne $webtime.hour){set-date $webtime;write-host "Changed Local Time" -f yellow}
An example of a Google Map Time Zone API reply
dstOffset : 3600 rawOffset : -28800 status : OK timeZoneId : America/Los_Angeles timeZoneName : Pacific Daylight Time
Let me know if you know of a better method of checking for time zones and time without using local computers or domain servers.
No comments:
Post a Comment