|
| 1 | +#Requires -RunAsAdministrator |
| 2 | +#Requires -Version 2 |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | + Set Date and Time (Commandline Tool) |
| 7 | + (c) 2021-2024 ZOBEC Consulting, Michal Zobec. All Rights Reserved. |
| 8 | + Licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) |
| 9 | +
|
| 10 | +.DESCRIPTION |
| 11 | + For single run get real date and time from internet service and configure date and time in running system. |
| 12 | + Can run on older systems like Microsoft Windows Server 2008, with PowerShell version 2. |
| 13 | +
|
| 14 | +.OUTPUTS |
| 15 | + Log file & text output. |
| 16 | +
|
| 17 | +.EXAMPLE |
| 18 | + C:\> Set-DateAndTime.ps1 |
| 19 | +
|
| 20 | +.NOTES |
| 21 | + Twitter: @michalzobec |
| 22 | + Blog : http://www.michalzobec.cz |
| 23 | +
|
| 24 | +.LINK |
| 25 | + http://www.michalzobec.cz |
| 26 | +
|
| 27 | +.LINK |
| 28 | + http://www.zobecconsulting.cz |
| 29 | +
|
| 30 | +.LINK |
| 31 | +About this script on my Blog in Czech |
| 32 | +http://zob.ec/ |
| 33 | +
|
| 34 | +.LINK |
| 35 | +Documentation (ReadMe) |
| 36 | +https://github.com/michalzobec/ |
| 37 | +
|
| 38 | +.LINK |
| 39 | +Release Notes (ChangeLog) |
| 40 | +https://github.com/michalzobec/ |
| 41 | +
|
| 42 | +#> |
| 43 | + |
| 44 | +# cls |
| 45 | +# Enable TLS 1.2 for communication (if needed) |
| 46 | +# [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 |
| 47 | + |
| 48 | +# Create an instance of WebClient |
| 49 | +$webClient = New-Object System.Net.WebClient |
| 50 | + |
| 51 | +# Download content from the API |
| 52 | +$timeInfo = $webClient.DownloadString("http://worldtimeapi.org/api/timezone/Europe/Prague") |
| 53 | + |
| 54 | +# Display the API response |
| 55 | +Write-Host "API response: $timeInfo" |
| 56 | + |
| 57 | +# Extract the date and time from the JSON response using a regular expression |
| 58 | +$time = $timeInfo | Select-String -Pattern '"datetime":"([^"]+)"' | ForEach-Object { |
| 59 | + $_.Matches[0].Groups[1].Value |
| 60 | +} |
| 61 | + |
| 62 | +# Convert the extracted time to a format acceptable by Set-Date |
| 63 | +$timeFormatted = Get-Date $time |
| 64 | + |
| 65 | +Write-Host "Formatted time: $timeFormatted" |
| 66 | + |
| 67 | +# Set the system date and time |
| 68 | +Set-Date -Date $timeFormatted |
0 commit comments