Getting the date and time from a remote system via WMI and the WIN32_LocalTime class
Posted by: Jerry Lees
Recently, after the time change I had to log into around 30 to 40 servers to check that the date was correct. This was a real pain, since it required me to actually log onto the server via terminal services and run a command prompt to issue a date command.
Unfortunately, I didn’t have a script written to do this remotely with WMI and I needed to get it done very quickly… so I had to do it the long, old fashioned, and unlazy way. Shortly after this I wrote the following function to check the date on a remote server using the WMI Win32_LocalTime class! Notice the class name is “Local Time”, and that’s just what it is… the local time of the system. However, if you run it on a remote system it returns the local time of the remote system!
Here is the function I wrote:
WScript.Echo GetDate(“.”)
Function GetDate(strcomputer)
on Error resume next
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)
Set ColDate = objWMIService.ExecQuery(“Select * from Win32_LocalTime”)
for each objDate in ColDate
GetDate = ObjDate.Month & “/” & ObjDate.Day & “/” & ObjDate.Year
Next
‘cleanup memory
objWMIService = Nothing
End Function




