function Get-NtpTimeByW32tm { # A function get time from ntpserver via w32tm command param([string]$NtpServer) $ret = & cmd /c "w32tm /stripchart /computer:$($ntpserver) /samples:1 /dataonly" $localTime = $ret | Select-String "現在の時刻は " $localTime = $localTime -Replace "現在の時刻は ", "" $localTime = $localTime -Replace " です。", "" $diffSec = $ret | Select-String -Pattern '([-+]?\d+\.\d+)s$' if ($diffSec -and $diffSec.Matches.Count -gt 0) { $offset = [double]$diffSec.Matches[0].Groups[1].Value return ([datetime]$localtime).AddSeconds($offset) $offset } else { return "Error: w32tm error" } } # End of Get-NtpTimeByW32tm function # Usage Examples Get-NtpTimeByW32tm -NtpServer "time.windows.com"