Sign in to follow this  
Followers 0
mlglobal

Query a remote computer's time

5 posts in this topic

I'm trying to query a remote computer's time. Does anyone have a reference for this?

Here is my situation. I'm a domain admin trying to run scripts on remote XP computers via task scheduler. I'm running into 2 issues.

1. If the remote computer's time is ahead of the system I'm launching the task from, their script doesn't run.

2. If I have to run a script on all computers in the domain, I have to schedule it to run 1 hour ahead of the current time.

I know that a simple query in WMI is possible but I want to be able to do it in AutoIT. Any help would be appreciated.

I also heard whispers about WMI hooks in the next version of AutoIT. Is this true?

Thanks!

Share this post


Link to post
Share on other sites



To answer 1 part of you post, the beta currently has objcreate, objevent, objget, objname functions, so yes wmi is possible in the beta, i use the wmi often.

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

I had to make something for work.. Maybe a bit to crude for you..

Chris

#Include <Constants.au3>
#NoTrayIcon

$g_szVersion = "TimeSync 1.1"
If WinExists($g_szVersion) Then Exit; It's already running
AutoItWinSetTitle($g_szVersion)


Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown.
;Opt("GUIOnEventMode", 1) 

$Server= iniread ("TimeSync.ini", "General", "Server", "labserver")
$ShowExit =StringLower (iniread ("TimeSync.ini", "General", "ShowExit", "yes")) 
$ShowTrayTip =StringLower (iniread ("TimeSync.ini", "General", "ShowTips", "no"))
$TimeWindow =StringLower (iniread ("TimeSync.ini", "General", "TimeWindow", "10"))

If StringisDigit ( $Timewindow  ) and $Timewindow > 0 and $Timewindow < 60 then
    
Else
    $TimeWindow = 10
    Endif

$RunNow = TrayCreateItem("Sync time with "& $Server)



if $ShowExit =  "yes" then 
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")
Else
    $ExitItem = 0
    EndIf

TraySetToolTip ( "Time Sync with " & $Server )


TraySetState()

Sync()

While 1
$msg = TrayGetMsg()
    Select
    Case $msg = 0
        if @min = 00 and @sec < $TimeWindow then 
    Sync()
    Endif
        Continueloop

        Case $msg = $RunNow

            Sync()
        
        
    case $Msg = $ExitItem
        Exit
        EndSelect
Wend


Func sync()
    
        
$time1 = @hour & @Min 

runwait ("net time \\"& $Server & " /set /yes","",@SW_HIDE )


$time2 = @hour & @Min 

    ;$Check = Ping ( $Server )
        if $time1 = $time2 then 
                $Check = Ping ( $Server ) 
                If $Check = 0 and $ShowTrayTip = "yes" then
                    TrayTip ( "Sync", "Time not changed." & @CRLF &  "Either, time is already syncronised or can not find " & $Server & @CRLF & $Server & " could be protected by a firewall" & @CRLF &  "Current set time is " & @hour & ":" & @min, 10 , 2 )  
                    elseIf $ShowTrayTip = "yes" and $time1 = $time2 then 
                    TrayTip ( "Sync", "No need for time change, time is already syncronised with " & $Server & @CRLF & "Current set time is " &  @hour & ":" & @min, 10 , 1 )   
                Endif
                    
            EndIf
            if $time1 <> $time2 and $ShowTrayTip = "yes" then TrayTip ( "Sync", "Time re-syncronised with " & $Server & @CRLF  & "Current set time is " &  @hour & ":" & @min, 10 , 1 )

EndFunc

Also uses an ini file called TimeSync.ini with the following in

[General]

Server=Labserver

ShowExit=no

ShowTips=no

TimeWindow=10

Share this post


Link to post
Share on other sites

example of the wmi query to get time from the machine, just change localhost to machine name

CODE

$wbemFlagReturnImmediately = 0x10

$wbemFlagForwardOnly = 0x20

$colItems = ""

$strComputer = "localhost"

$Output=""

$Output = $Output & "Computer: " & $strComputer & @CRLF

$Output = $Output & "==========================================" & @CRLF

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _

$wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then

For $objItem In $colItems

$Output = $Output & "LocalDateTime: " & WMIDateStringToDate($objItem.LocalDateTime) & @CRLF

if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop

$Output=""

Next

Else

Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_OperatingSystem" )

Endif

Func WMIDateStringToDate($dtmDate)

Return (StringMid($dtmDate, 5, 2) & "/" & _

StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _

& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))

EndFunc


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Thanks to all!

Mat

Share this post


Link to post
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
Sign in to follow this  
Followers 0