RSS

WMI: Get Server Date Time

07 Aug

I used this vbscript function because I needed to get the date/time on a remote server to schedule some jobs on it. It works for both Windows 2000 and Windows 2003, don’t know about NT, have not tried it on NT yet.

It returns a date/time value in the following format: dd/mm/yyyy hh:mm:ss

You can use various vbscript functions to manipulate or extract the date/time. For example:

dtmSrvDateTime = GetSrvDateTime(ServerName)

dtmNewDateTime = DateAdd(“n”, 2, dtmSrvDateTime)

dtServerDate = FormatDateTime(dtmTaskDateTime, vbShortDate)

‘==========================================
Function GetSrvDateTime(ByVal ServerName)
‘==========================================

Dim objWMIService
Dim OSVersion
Dim colOS, objOS
Dim colItems, objItem
Dim colLocalTime, oLocalTime

Dim dtmLocalTime
Dim dtmMonth, dtmDay, dtmYear
Dim dtmHour, dtmMinutes, dtmSeconds

Dim ServerDate, ServerTime

Set objWMIService = GetObject(“winmgmts:\\” & ServerName & “\root\cimv2”)

Set colOS = objWMIService.ExecQuery(“SELECT * FROM Win32_OperatingSystem”)
For Each objOS in colOS
OSVersion = objOS.Caption
Next

if Instr(OSVersion, “2000”) > 0 then

Set colItems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)

For Each objItem in colItems

dtmLocalTime = objItem.LocalDateTime
dtmMonth = Mid(dtmLocalTime, 5, 2)
dtmDay = Mid(dtmLocalTime, 7, 2)
dtmYear = Left(dtmLocalTime, 4)
dtmHour = Mid(dtmLocalTime, 9, 2)
dtmMinutes = Mid(dtmLocalTime, 11, 2)
dtmSeconds = Mid(dtmLocalTime, 13, 2)

Next

ServerDate = dtmDay & “/” & dtmMonth & “/” & dtmYear
ServerTime = dtmHour & “:” & dtmMinutes & “:” & dtmSeconds

GetSrvDateTime = CDate(ServerDate & ” ” & ServerTime)

else

Set colLocalTime = objWMIService.ExecQuery(“Select * from Win32_LocalTime”)

Set GetSrvDateTime = Nothing
for each oLocalTime in colLocalTime

ServerDate = oLocalTime.Day & “/” & oLocalTime.Month & “/” _

& oLocalTime.Year
ServerTime = oLocalTime.Hour & “:” & oLocalTime.Minute
GetSrvDateTime = CDate(ServerDate & ” ” & ServerTime)

exit for

next

end if

End Function

Advertisements
 
Leave a comment

Posted by on August 7, 2007 in Scripts, VBScripts, Windows

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: