I ran into an issue lately where the domain controller that hosts the PDC emulator FSMO role for the forest root domain became unavailable and the time for several machines in the domain was far enough off to start causing kerberos related security problems.
Here’s a simply Powershell script to query the time on remote machines via WMI. You could use Invoke-Command with Get-Date, but that takes too long compared to just using WMI. I chose to hard code the names I wanted to query directly into the script to eliminate having to keep up with a separate text file or trying to query Active Directory for the names since that may not be possible if the time is way off. This script is my solution to the “I have a problem and I want to eliminate the time on remote machines as a source of it”. I want the time and I want it now!
1 2 3 4 5 6 | $servers = 'server1', 'server2', 'server3', 'server4', 'server5', 'server6' ForEach ($server in $servers) { $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime) $server + ' ' + $time } |
µ
handy script…and get-date via powershell is MUCH more efficient than the old net time query I was running from cmd…thanks!
Not sure this script is working correctly. I ran it against remote servers and it reported back the times in CST. When I logged into some of them, clicked on Date/Time it actually showed EST.
Just something you will want to add in, in case it can’t reach a “server”. the first line in the For loop should reset the time variable. i used: $time = ‘bad’
where can i insert the convertto-csv command?, on that given script
Great script , can anyone help me to make it
import server names from csv ?
$servers = Get-Content -Path “c:\computernames.csv”
you should add this at the first line of your script. “c:\computernames.csv” should be the complete patch where you have seved your csv file
is there a way to output to a txt file? I tried using
Out-File c:\scripts\sys4.txt
it creates the file but does not populate the file with the data, I’m really new to scripting and I’m sure I’m missing something pretty simple.
thanks
$time 2>&1 | out-file ‘fileName.txt’ -append
Tried to read from remote computer but facing below issue
gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At D:\development\Time_zone.ps1:4 char:42
+ … tToDateTime((gwmi win32_operatingsystem -computername $server).LocalD …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please let me know if we want to poll data from remote server, how
is there a way to output to a txt file?
$time 2>&1 | out-file ‘fileName.txt’ -append