• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2441
  • Last Modified:

vbscript to get remote resource time

Similar to net time \\server
How could I do this via a vbs?
0
sirbounty
Asked:
sirbounty
  • 4
  • 3
  • 2
  • +1
1 Solution
 
b0lsc0ttCommented:
sirbounty,

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

b0lsc0tt
0
 
b0lsc0ttCommented:
Actually just the command time() will work.
0
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
b0lsc0ttCommented:
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
 
tone28Commented:
' 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
 
sirbountyAuthor Commented:
Fourth time's a charm... ;^)
Thanx - works great.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now