vbscript to get remote resource time

Similar to net time \\server
How could I do this via a vbs?
LVL 67
sirbountyAsked:
Who is Participating?
 
tone28Connect With a Mentor Commented:
' OK last time. Man.... going to bed now.

WScript.echo ServerTime("NameOfServer")

Function ServerTime(serverName)

    Set wmi = GetObject("winmgmts://" & serverName & "/root/cimv2")

    Set op = wmi.ExecQuery("Select * From Win32_LocalTime")
    Dim Day, DayOfWeek, Month, Hour, Milli, Minute, Second, WeekInMonth, Year
     
    For Each ele In op
     Day = ele.Day
     Hour = ele.Hour
     Month = ele.Month
     Minute = ele.Minute
     Year = ele.Year
    Next
     
    Dim AmPm
   
    If Hour > 12 Then
     AmPm = "PM"
     Hour = Abs(Abs(Hour - 24) - 12)
    Else
     If Hour = 0 Then
         Hour = 12
     End If
        AmPm = "AM"
    End If
    If Minute < 10 Then
        Zero = 0
    Else
        Zero = ""
    End If    
    ServerTime =  "Current time at " & serverName & " is " & _
                Month & "/" & Day & "/" & Year & _
             " " & Hour & ":" & Zero & Minute & " " & AmPm & vbCrLfv
End Function
0
 
b0lsc0ttIT ManagerCommented:
sirbounty,

The commands now() or time(now()) should do it.

b0lsc0tt
0
 
b0lsc0ttIT ManagerCommented:
Actually just the command time() will work.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
sirbountyAuthor Commented:
Not sure I follow you.
If I run this from my workstation, how do I find out the current local time (traversing time zones) of a workstation 2000 miles away?
0
 
b0lsc0ttIT ManagerCommented:
sirbounty,

Sorry, I may have misunderstood what you were trying to do.  My command would work if in a file on the remote (web) server using ASP technology.  If I understand you correctly now then my suggestion will not help you.

bol
0
 
itdrmsCommented:
Maybe not the most elegant: but it works
If you want to synch to that remote server, it would be cleaner -- but your question sounded like you just wanted to retrieve the time.

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
Dim sTime
WSHShell.Run "cmd /c net time \\server|find ""Current"" > c:\temp\temp.txt"
set WSHShell = Nothing

Dim oFSO, oTimeFile, TextLine
Set oFSO = CreateObject("Scripting.fileSystemObject")
Set oTimeFile = oFSO.OpenTextFile("c:\temp\temp.txt", 1)

TextLine = oTimeFile.Readline

oTimeFile.Close
Set oFSO = Nothing

msgbox right(TextLine,8)


 

0
 
tone28Commented:
Here is a verion using WMI

WScript.echo ServerTime("NameOfServer")

Function ServerTime(serverName)

    Set wmi = GetObject("winmgmts://" & serverName & "/root/cimv2")

    Set op = wmi.ExecQuery("Select * From Win32_LocalTime")
    Dim Day, DayOfWeek, Month, Hour, Milli, Minute, Second, WeekInMonth, Year
      
    For Each ele In op
      Day = ele.Day
      Hour = ele.Hour
      Month = ele.Month
      Minute = ele.Minute
      Year = ele.Year
    Next
      
    Dim AmPm
      
    If Hour > 12 Then
        AmPm = "PM"
        Hour = Abs(Abs(Hour - 24) - 12)
    Else
        If Hour = 0 Then
            Hour = 12
        End If
        AmPm = "AM"
    End If
      
    ServerTime =  "Current time at " & serverName & " is " & _
                 Month & "/" & Day & "/" & Year & _
               " " & Hour & ":" & Minute & " " & vbCrLfv
End Function
0
 
tone28Commented:
Correction


WScript.echo ServerTime("nhq1accodc02")

Function ServerTime(serverName)

    Set wmi = GetObject("winmgmts://" & serverName & "/root/cimv2")

    Set op = wmi.ExecQuery("Select * From Win32_LocalTime")
    Dim Day, DayOfWeek, Month, Hour, Milli, Minute, Second, WeekInMonth, Year
      
    For Each ele In op
      Day = ele.Day
      Hour = ele.Hour
      Month = ele.Month
      Minute = ele.Minute
      Year = ele.Year
    Next
      
    Dim AmPm
    Minute = 8      
    If Hour > 12 Then
      AmPm = "PM"
      Hour = Abs(Abs(Hour - 24) - 12)
    Else
      If Hour = 0 Then
          Hour = 12
      End If
        AmPm = "AM"
    End If
    If Minute < 10 Then
        Zero = 0
    Else
        Zero = ""
    End If      
    ServerTime =  "Current time at " & serverName & " is " & _
                 Month & "/" & Day & "/" & Year & _
               " " & Hour & ":" & Zero & Minute & " " & vbCrLfv
End Function
0
 
tone28Commented:
' Dang.... too late for me. Another correction

WScript.echo ServerTime("nhq1accodc02")

Function ServerTime(serverName)

    Set wmi = GetObject("winmgmts://" & serverName & "/root/cimv2")

    Set op = wmi.ExecQuery("Select * From Win32_LocalTime")
    Dim Day, DayOfWeek, Month, Hour, Milli, Minute, Second, WeekInMonth, Year
      
    For Each ele In op
      Day = ele.Day
      Hour = ele.Hour
      Month = ele.Month
      Minute = ele.Minute
      Year = ele.Year
    Next
      
    Dim AmPm
   
    If Hour > 12 Then
      AmPm = "PM"
      Hour = Abs(Abs(Hour - 24) - 12)
    Else
      If Hour = 0 Then
          Hour = 12
      End If
        AmPm = "AM"
    End If
    If Minute < 10 Then
        Zero = 0
    Else
        Zero = ""
    End If      
    ServerTime =  "Current time at " & serverName & " is " & _
                 Month & "/" & Day & "/" & Year & _
               " " & Hour & ":" & Zero & Minute & " " & vbCrLfv
End Function
0
 
sirbountyAuthor Commented:
Fourth time's a charm... ;^)
Thanx - works great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.