Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get System Time Remotely 3

Status
Not open for further replies.

twooly

MIS
Joined
Feb 22, 2004
Messages
122
Location
US
Anyone have ideas on how to get the system time of servers remotely? I'm think I may use the net time \\server and parse that out.
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem in colItems
Wscript.Echo "Year: " & objItem.Year
Wscript.Echo "Month: " & objItem.Month
Wscript.Echo "Day: " & objItem.Day
Wscript.Echo "Hour: " & objItem.Hour
Wscript.Echo "Minute: " & objItem.Minute
Wscript.Echo "Second: " & objItem.Second
Wscript.Echo "Quarter: " & objItem.Quarter
Wscript.Echo "Week in the Month: " & objItem.WeekInMonth
Wscript.Echo "Day of the Week: " & objItem.DayOfWeek
Next

[yinyang] Tranpkp [pc2]
 
I just love all the ways things can be done. Here's what I did
Code:
Function GetTime(vServer)
    On Error Resume Next
    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1

    Set oShell = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sTempFile = "C:\runresult.tmp"

    oShell.Run "%comspec% /c net time \\" & vServer & ">" & sTempFile, 0, True

    Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)

    sResults = fFile.ReadLine
    fFile.Close
    oFSO.DeleteFile (sTempFile)
            
    aTime = Split(sResults, "is")
       
    GetTime = aTime(1)
    
    Set oShell = Nothing
    Set oFSO = Nothing
End Function
 
Big Star for dm4ever. In response to tranpkp, that script works only on 2003/xp as there is no Win32_LocalTime object in Windows2000 WMI. Here is the code for Windows 2000.
Code:
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
 
For Each objItem in colItems
    strTime = objItem.LocalDateTime
    dtmTime = CDate(Mid (strTime, 9, 2) & ":" & Mid(strTime, 11, 2) & ":" & _
        Mid(strTime, 13, 2))
    dtmTime = CDate(dtmTime)
    Wscript.Echo FormatDateTime(dtmTime, vbFormatLongTime)
Next
 
Thanks! It looks like MS has moved the location of the link referenced above. Here is where it went:
--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top