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

随声附和 提交于 2020-01-05 09:04:21

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/9798009/query-domain-controllers-ntp-servers-time-w32tm-monitor-format-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!