Why get time?
I am working in Windows shop for process automation industry. And we have very strict rule on time synchronization
On SCADA system time synchronization is crucial in keeping the data clean. Usually system can tolerate several seconds of lagging or advance. However, the castle will start to crumble if there’s too much of time difference.
How is it critical?
When there’s maintenance on the Historical data server (Historian), the live-data will be stored in communication server. Once Historian is back to operation and communication server is able to connect to Historian server, the stored data will be pushed. Data being pushed can be considered “not-original” if there’s any existing “time-stamped” data in Hitsorian.
Sample case:
- Historian went for maintenance on 14:15:01 (Historian server time)
- Communication server is at 14:10:23 (around 5 mins lagging), start to keep value since Historian server is not reachable
- Historian server come back 15 mins later, at 14:30:20
- Communication server will forward the data with timestamp of 14:10:23 to 14:25:45
- Historian already have data timestamped prior to 14:15:00, thus, may or may not omit the data overlapped in point above.
- In this case, data for time 14:25:45 to 14:30:20 will be treated as NULL value, which is not the case
or
When Historian server is lagging several minutes behind communication server, the data may be taken as “bad” value. Sample case:
- Current Historian server time 14:01:01
- Current Communication server time 14:15:23
- Current temperature read from PLC: 36.6°C
- When communication server update temperature to Historian server, the data sent will have metadata of time attached.
In this case:- Tagname: Temperature
- Value: 36.6°C
- Time: 14:15:23
In the case above, some Historian will take the value as “bad” value as time stamp is several minutes in advance (i.e. it can be to prevent spoofing of data)
When is it critical?
It become critical when there’s issue with a piece of equipment, and we are relying on Historical data to shed some light on the root cause.
For example in a production, temperature for equipment A is slowly decreasing when it’s supposed to maintain. What engineers will do is open the Historical trend for the said production timeline and maybe compare it to the previous run. If there’s any time discrepancy, there will be higher chance that they’ll get either NULL data, or inconsistent data, hindering engineers from finding root cause.
Get time from a list of computers
The following script has been tested on domain joined computers. You will need user account which are able to connect remotely through PowerShell (i.e. elevated)
# Computer List, can be populated with a list from file
# $computers = Import-Csv C:\Computer-list.csv
$computers = @{'computer-01','computer-02','server-01'}
# Declare variable to contain results
$result = @{}
# Request time from remote computers and arrange into new array element
ForEach ($computer in $computers){
$RemoteTime = Invoke-Command -ComputerName $computer {
Get-Date -Format 'HH:mm:ss'
}
$LocalTime = Get-Date -Format 'HH:mm:ss'
$result += New-Object -TypeName PSCustomObject -Property (@{
'Server' = $computer
'RemoteTime' = $RemoteTime
'LocalTime' = $LocalTime
})
}
# You can export or just show the result
$result
Time Sync
Query time source
We can run the following in PowerShell command to check source
# Get status from Windows Time Service
# Will return the status of Active Directory time sync service
# What we are interested is "Source" section
w32tm /query /status
# --- NEED ELEVATED FOR LINE BELOW ---
# To check which default method is used for time sync
w32tm /query /configuration
Configure Time Source
We can run the following in elevated PowerShell to configure time source
# Use Windows Time service to configure source NTP server
# Will need to restart w32time service to update configuration
# For a list of available NTP server in the internet, can go to http://www.pool.ntp.org/zone/sg
w32tm /config /manualpeerlist: 0.sg.pool.ntp.org /syncfromflags:manual /reliable:yes /update
Stop-Service w32time
Start-Service w32time
# To query configured source time server
w32tm /query /source
Force Time Sync
To force time sync, we’ll need to run in elevated PowerShell
# Windows Time service resync to configured time server
w32tm /resync