Jump to content

Time Sync all computers in domain or workgroup


Recommended Posts

This is not much, guess it can be adapted though for various things. Maybe give some people ideas....

If your like me and do not like some of the windows services (w32time) because they never seem to work properly and fill up the event log then you could use something like this and just disable the service.

The script uses psexec that should be in the same directory as the script.

You will need to change the user name and password in the RunPsExec() function.

The main purpose is to be able to run this script from one computer (server maybe), you could run it as a service if you want with srvany.exe. (note NET TIME will not work if you use the SYSTEM account to run it)

It gets all the computers in the workgroup (or domian with some small tweaks) with NET VIEW, then runs the NET TIME on them via psexec.exe.

Note, this only maybe be helpful in some situations. eg. On our computers we have the same account setup (user and password) so psexec is easy to run. Psexec also has its limitations.

So basically this is just to give people some ideas from a bit a code. Run other programs remotely automatically from one computer etc....

#include <Constants.au3>
#include <String.au3>

Enum $E_STD_OUT = 1, $E_STD_ERR
Const $sNetViewCmd = 'NET VIEW', $sNetTimeCmd = 'NET TIME \\' & @ComputerName & ' /SET /YES'
Const $iDelay = 1000 * 60 * 10
Dim $iPID = 0, $aOutput = 0, $aComputers = 0

While 1
    $iPID = Run(@ComSpec & ' /c ' & $sNetViewCmd, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    $aComputers = GetNetworkComputers($aOutput)
    RunPsExec($sNetTimeCmd, $aComputers)

Func GetConsoleOutput($iPID)
    Local $sStr = '', $cDelim = 'À'    
    While 1
        $sStr &= StdoutRead($iPID)
        If @error Then ExitLoop
    $sStr &= $cDelim
    While 1
        $sStr &= StderrRead($iPID)
        If @error Then ExitLoop
    Return StringSplit($sStr, $cDelim, 1)

Func GetNetworkComputers($aOutput)
    Local $aArray = 0
    $aOutput = GetConsoleOutput($iPID)
    $aArray = _StringBetween($aOutput[$E_STD_OUT], Chr(92) & Chr(92), @CRLF)
    For $i = 0 To UBound($aArray) - 1 Step 1
        $aArray[$i] = StringStripCR($aArray[$i])
        $aArray[$i] = StringStripWS($aArray[$i], 8)

    Return $aArray

Func RunPsExec($sCmd, $aArray)
    Const $psExecExe = 'psexec.exe'
    Const $sJitUserName = 'test', $sJitUserPassword = '****'
    Local $sPsExecCmdTemplate = $psExecExe & ' \\\\' & '%s -u "%s" -p "%s" -d ', $sPsExecCmd = ''
    For $i = 0 To UBound($aArray) - 1 Step 1
        $sPsExecCmd = StringFormat($sPsExecCmdTemplate, $aArray[$i], $sJitUserName, $sJitUserPassword) & $sNetTimeCmd
        Run(@ComSpec & ' /c ' & $sPsExecCmd, @ScriptDir, @SW_HIDE)
Edited by creeping
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...