Domain controller NTP Source and time synchronization check

Created 2 scripts, one that will check which time source is being used, and one that measures the time difference. But now it want to combine those script and i'm not sure how to do it. I want the output to be something like :

DC01 optimal time synchronisation, uses DC02 as the timesource.

Timecheck difference script

$servers = "DC01","DC02","DC04"

$w32tm = invoke-command -computername $servers -scriptblock{w32tm /monitor /computers:$servers /nowarn}

        $icmp = ($w32tm -like "*ICMP*") -replace "ICMP:",""

 

            If($icmp -le "0ms"){$timestatus="Optimal time synchronisation"}

            IF($icmp -gt "300000ms"){$timestatus="Critical. Over 5 minutes time difference!"}

            If($icmp -gt "100000ms"){$timestatus="Warning, 2 minutes time difference"}

 

 

foreach($server in $servers){

Write-Host $timestatus for $server

}

NTP Source script

$Computername = "dc01","dc02","dc04"

 

 

    foreach ($Computer in $Computername)

    {

        $ntps = w32tm /query /computer:$Computer /source

        new-object psobject -property @{

            Name = $Computer

            NTPSource = $ntps

        }

    }

 

  • $Servers = 'localhost','127.0.0.1'
    
    $w32tm = Invoke-Command -Computer $Servers -ArgumentList $Servers -Scriptblock {
        Param ($Servers)
        Foreach ($Server in $Servers)
        {
            $Check = w32tm /monitor /computers:$Server /nowarn
            $ICMP = (($Check | Select-String "ICMP")-Replace "ICMP: " , "").Trim()
            $ICMPVal = [int]($ICMP -split "ms")[0]
            $Source = w32tm /query /source
            $Name = Hostname
    
            Switch ($ICMPVal)
                {
                    {$ICMPVal -le 0} {$Status = "Optimal time synchronisation"}
                    #you probably need another value here since you'll get no status if it is between 0 and 2m
                    {$ICMPVal -lt 100000} {$Status = "0-2 Minute time difference"}
                    {$ICMPVal -ge 100000} {$Status = "Warning, 2 minutes time difference"}
                    {$ICMPVal -ge 300000} {$Status = "Critical. Over 5 minutes time difference!"}
                }
            $String = $Name + " - $Status " + "- $ICMP " + " - Source: $Source"
            Write-Output $String
        }
    }
    
    $w32tm
    
  • Thanks for posting the script. Works great, but now i want the results to be displayed in a html file. When i run the command it only displays these values in powershell. It has something to do with the invoke command but i have no clue how to convert it to readable values. 

    Also the output when running $w32tm | format-list * -force

    Temporary exported to a text file and then used the select-string to get the lines i need. But is their another way to do it?

    PSComputerName
    RunspaceId
    PSShowComputerName
    Length
  • Well, if you just print $w32tm you're getting what it stored in lets say "the root" of the variable.

    When you pipe to format-list * you're telling it to instead print all available properties. Oddly the root value is not stored as a sub property which is why it is not displaying...I don't have a good explanation as to why.

    So the answer is to not choose what $w32tm displays and you won't run into the problem, you can't do anything really with the formatting anyways since it is just strings. If you want it to be html do this.

     

    $w32tm | ConvertTo-HTML > C:\test.html