windowsbatch-filepowershellntp

Get NTP IP in windows servers


I kind of giveing up to find NTP IP address in 200 windows servers 2003 and 2008. I tried powershell script (Here) and (Here)

Still I couldn't get any NTP ip. When I run w32tm /query /status in command prompt, I will get

Leap Indicator: 0(no warning) Stratum: 5 (secondary reference - syncd by (S)NTP) Precision: -6 (15.625ms per tick) Root Delay: 0.1293793s Root Dispersion: 0.2016004s ReferenceId: 0x00000000 (source IP: 192.xxx.xxx.xxx) Last Successful Sync Time: 7/15/2014 3:39:07 PM Source: Host.LocalDomain.com Poll Interval: 12 (4096s)

I need some batch/shell that I could import severs list (windows 2003/2008/2008R2) and output would be source IP. I just want to make sure all my servers are using same IP address. That would be great if I could use other credential.

Thank you for your help.


Solution

  • This will be returning the IP of NTP server. Prerequisites: RemoteRegistry running on the server and of course access to the remote registry.

    $Serverlist = cat $env:userprofile\Desktop\Test.txt
        foreach ($Server in $Serverlist) {
        $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Server)
        $objKey = $Reg.OpenSubKey("SYSTEM\\CurrentcontrolSet\\Services\\W32Time\\Parameters")
        $DNSNTP = $objKey.GetValue("NtpServer")
        $DNSNTP = ($DNSNTP = $DNSNTP.Split(','))[0]
        ([System.Net.DNS]::GetHostAddresses($DNSNTP)) | Select -ExpandProperty IpAddressToString
        }
    

    Edit:

    Since it seems I misunderstood the question

    Here is something that returns exactly the referenceID field from W32TM. This will however extract the data from the FIXED point. if something is different in your setup, you will have to manuever around calling the array elements, splits and substrings. On the other hand, the referenceID for me returns the IP address of ntp server if there is a domain one set... Plus, you have to have Enable-PSremoting -force set up on the servers for them to accept calls.

    $Serverlist = cat $env:userprofile\Desktop\Test.txt
    foreach ($Server in $Serverlist) { Invoke-Command -Command {(w32tm /query /status)[5].split(' ')[-1].TrimEnd(')')} -Computername $Server