0

In Windows, when you are updating your date and time through a server where is this variable stored?

I am writing a PowerShell script that requires that I check this variable but I can't seem to find out how to derive the data.

enter image description here

JoeCodeCreations
  • 660
  • 2
  • 6
  • 19

2 Answers2

1

You should be able to get the information from the registry.Try this:

$key = 'HKLM:\SYSTEM\CurrentControlSet\services\W32Time\Parameters'

$enabled = switch ((Get-ItemProperty $key).Type) {
             { 'NTP','NT5DS','AllSync' -contains $_ } { $true }
             'NoSync' { $false }
             default { throw "Invalid type value $_." }
           }

You could also work with a hashtable instead of a switch statement:

$types = @{
  'NTP'     = $true
  'NT5DS'   = $true
  'AllSync' = $true
  'NoSync'  = $false
}
$key = 'HKLM:\SYSTEM\CurrentControlSet\services\W32Time\Parameters'

$enabled = $types[(Get-ItemProperty $key).Type]

Note that per the Windows Time Service documentation the Type entry can have 4 different values, 3 of which indicate that synchronization is enabled.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    I believe I found it using this post as help. I see that the parameters found as Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" hold a Type= where NoSync if off or NTP if network server sync – JoeCodeCreations Jul 29 '15 at 18:46
0

First link from google "powershell get ntp settings"

Pasting code here (Credit to Jeff Wouters)

function Get-TimeServer {
<#
.Synopsis
Gets the time server as configured on a computer.

.DESCRIPTION
Gets the time server as configured on a computer.
The default is localhost but can be used for remote computers.

.EXAMPLE
Get-TimeServer -ComputerName "Server1"

.EXAMPLE
Get-TimeServer -ComputerName "Server1","Server2"

.EXAMPLE
Get-TimeServer -Computer "Server1","Server2"

.EXAMPLE
Get-TimeServer "Server1","Server2"

.NOTES
Written by Jeff Wouters.
#>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param ( 
        [parameter(mandatory=$true,position=0)][alias("computer")][array]$ComputerName="localhost"
    )
    begin {
        $HKLM = 2147483650
    }
    process {
        foreach ($Computer in $ComputerName) {
            $TestConnection = Test-Connection -ComputerName $Computer -Quiet -Count 1
            $Output = New-Object -TypeName psobject
            $Output | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $Computer
            $Output | Add-Member -MemberType 'NoteProperty' -Name 'TimeServer' -Value "WMI Error"
            $Output | Add-Member -MemberType 'NoteProperty' -Name 'Type' -Value "WMI Error"
            if ($TestConnection) {              
                try {
                    $reg = [wmiclass]"\\$Computer\root\default:StdRegprov"
                    $key = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
                    $servervalue = "NtpServer"
                    $server = $reg.GetStringValue($HKLM, $key, $servervalue)
                    $ServerVar = $server.sValue -split ","
                    $Output.TimeServer = $ServerVar[0]
                    $typevalue = "Type"
                    $type = $reg.GetStringValue($HKLM, $key, $typevalue)
                    $Output.Type = $Type.sValue             
                    $Output
                } catch {
                }
            } else {
            }
        }
    }
}
Eric Walker
  • 1,042
  • 10
  • 15