Query domain controllers & NTP servers time w32tm /monitor format output

user1131196 picture user1131196 · Mar 21, 2012 · Viewed 10.1k times · Source

I'm using the following to measure the time offset between our domain controllers and ntp servers.

$Servers = "ntp.xxxxx,ntp.xxxxx,dc1,dc2,dc3,dca,dcb,dcc"
$ListDomains = "domain1","domain2"

Foreach ($Server in $ListServers) {
    $time = (w32tm /stripchart /dataonly /computer:$Server /samples:1)[-1].split("[")[0]
    "$Server`: `t $Time" #| out-file $timeFile -append
    $time = ""  
} 

ForEach ($Domain in $ListDomains) {
    "** $Domain **"
    w32tm /monitor /domain:"$Domain.unisa.edu.au" /nowarn /threads:5
}

This is working but the output is horrible. Domain 1

itupw-xxxxx.xxxxxxxxxxxxxx[666.666.6.76:123]:
    ICMP: 0ms delay
    NTP: -0.0099384s offset from itupw-xxxxx.xxxxxxxxxxxxxx
     RefID: itupw-xxxxx.xxxxxxxxxxxxxx[22222222222222]
        Stratum: 5
itupw-xxxxx.xxxxxxxxxxxxxx[999.666.6.76:123]:
    ICMP: 0ms delay
    NTP: -0.0093544s offset from itupw-xxxxx.xxxxxxxxxxxxxx
        RefID: itupw-xxxxx.xxxxxxxxxxxxxx[22222222222222]
        Stratum: 5

Can anyone please suggest a way of formatting this so the data is easier to compare? We're only interested in Name, ICMP, NTP(offset).

As the NTP boxes are Solaris we can't use WMI queries.

Thanks, Amelia

Answer

Andy Arismendi picture Andy Arismendi · Mar 21, 2012

Gives this a try. It reads w32tm stdout and parses it into custom objects and puts them into an array. You can just process the array like any other collection of objects.

    $output1 = & w32tm /monitor /domain:yourdomain.com /threads:5
    $stdOutStart = 8
    $output = $output1[$stdOutStart..$output1.Length]
    $timeInfos = @()

    for ($i = 0 ; $i -lt $output.Length ; $i+=4) {
        $server = $output[$i].Split(' ')[0]
        $icmp = $output[$i+1].Trim().Split(' ')[1]
        $offset = $output[$i+2].Trim().Split(' ')[1]
        $timeInfos += New-Object PsObject -Property @{
            Server = $server
            ICMP = $icmp
            Offset = $offset
        }
    }

    $timeInfos