# How Can I Verify the System Time on a Remote Computer?

### How Can I Verify the System Time on a Remote Computer?

• Likes

Hey, Scripting Guy! How can I verify the system time on a remote computer?

-- MN

Hey, MN. You know, recently the Scripting Guy who writes this column was reading about the “law of attraction,” which is supposedly a natural law of the universe that, if you understand how to use it, can give you anything your heart desires. To quote from a recent book on the subject:

"The law of attraction is a law of nature. It is impersonal and it does not see good things or bad things. It is receiving your thoughts and reflecting back to you those thoughts as your life experience. The law of attraction simply gives you whatever it is you are thinking about."

 Note. Yes, there’s always a catch: the law of attraction gives you whatever it is you are thinking about. No wonder the Scripting Guys never get what they want!

Now, to be honest, the Scripting Guy who writes this column tends to be a bit skeptical. The earth is round? Well, maybe. By using a microwave you can cook an entire burrito in less than a minute? That seems too good to be true. The law of attraction simply gives you whatever it is you are thinking about? Hmmm ….

Interestingly enough, however, it was shortly after reading about the law of attraction that the Scripting Guy who writes this column actually started thinking: “I don’t know how to verify the system time on a remote computer,” he thought. “And I don’t want to waste my afternoon trying to figure out how I can do it. I’m thinking it would be nice if the universe would just give me a script that can verify the system time on a remote computer.”

Well, lo and behold:

```strComputer = "."

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

Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")

For Each objItem in colItems
strTime = objItem.Hour & ":" & objItem.Minute & ":" & objItem.Second
dtmTime = CDate(strTime)
Wscript.Echo FormatDateTime(dtmTime, vbFormatLongTime)
Next
```

Kind of spooky, isn’t it?

Best of all, this isn’t a bad little script; in fact, it’s even a bit fancier than anything the Scripting Guys were likely to come up with. Not that it’s perfect, mind you; after all, it only runs on Windows XP and subsequent versions of Windows. (Apparently the universe doesn’t realize that some people are still using Windows 2000.) But that’s OK; before we call it a day we’ll show you an alternate method for getting the system time that will work on Windows 2000. That’s the least we could do, seeing as how it was probably our muddled thinking that led the universe to ignore Windows 2000 users in the first place.

Speaking of the universe, its script starts off by connecting to the WMI service on the local computer. But wait, you ask: didn’t we want to get the system time on a remote computer? Don’t worry; the universe has you covered. To get the time on a remote computer simply assign the name of that machine to the variable strComputer, like so:

```strComputer = "atl-fs-01"
```

After connecting to the WMI service we then use the following query to return all the instances of the Win32_LocalTime class:

```Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")
```

Incidentally, this explains why the universe’s script won’t work on Windows 2000: the Win32_LocalTime class wasn’t introduced until Windows XP.

As you might expect, Win32_LocalTime is a “singleton” class; that means it returns only one item. (In general, a computer can only have one system time. We say “in general” because Scripting Guy Peter Costantini insists that on his home planet, located somewhere in the 35th dimension, computers can have more than one system time. Needless to say, we’re a bit skeptical about that, too.) However, even though we only have one item the ExecQuery method always returns information in the form of a collection; in turn, that means we still have to set up a For Each loop to loop through all the items in the collection:

```For Each objItem in colItems
```

And what is the universe going to have us do inside this loop? Well, for starters, we’re going to grab the values of the Hour, Minute, and Second properties and string them together, storing the result in a variable named strTime:

```strTime = objItem.Hour & ":" & objItem.Minute & ":" & objItem.Second
```

To tell you the truth, the Scripting Guys would have called it good at this point and simply echoed back the value of strTime. However, the Win32_LocalTime class uses a 24-hour clock; that means that 2:00 PM is actually rendered as 14:00. If you don’t mind the 24-hour format then you’re done. The universe, however, decided to add a couple extra lines of code in order to format this as a standard time.

What do we mean by that? Well, suppose the local time on the computer is 2:33:16 PM; that means strTime will be equal to this

```14:33:16
```

In order to convert this to a standard time format the universe first uses the CDate function to convert the character value 14:33:16 to a date-time value:

```dtmTime = CDate(strTime)
```

After that the universe then uses the FormatDateTime function to echo the value of dtmTime using the time settings specified in your computer’s Regional and Language Settings. (That’s what the VBScript constant vbLongTme does.) On the Scripting Guys’ test computer, that’s going to look like this:

```2:33:16 PM
```

That’s a little nicer, especially if you aren’t comfortable with the 24-hour clock.

Well, it’s nicer as long as you’re not using Windows 2000; if you are, the preceding script won’t help you much. But don’t despair: Windows 2000 types can always use the Win32_OperatingSystem class and the LocalDateTime property to determine the system time. The LocalDateTime property stores time using the infamous UTC (Universal Time Coordinate) format, but we can do some fancy footwork and convert these UTC values to something a bit more user-friendly:

```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
```

We won’t discuss this script in any detail today, but you can learn more about the UTC time format by thumbing through the Microsoft Windows 2000 Scripting Guide.

As for the Scripting Guys, we’re assuming this is our last column; that’s because we’re thinking we’d each like to have 10 million dollars so that we can all retire. If this law of attraction thing really works, well, tomorrow we’ll all be lying on the beach drinking Mai Tais. It’s been fun, but all good things must come to an end

Well, unless some of you are thinking, “Gee, I hope there’s a Hey, Scripting Guy! column tomorrow.” How does the law of attraction deal with a situation where one person thinks X and another person thinks Y? We’ll have to think about that one.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
• My screen, in Firefox, did not "paint" completely - upon refreshing it, I see you DID say to use "remote-computer" in the script! Touche!

Still, my script is much prettier. LOL.

• Very good script and very good explanation

• Powershell is the latest?  Care to update this article with some new code?

• gwmi Win32_LocalTime -computer remotepc |

select @{

N='LocalTime';

E={

get-date -year \$_.year -month \$_.Month -day \$_.day -hour \$_.Hour -minute \$_.minute

}

}

• well this is great but what if I need to run this on multiple servers, this script is awesome. and would be a great asset to have for a large environment.

Thanks can always rely on you for making my life easier.

• what does each item in collection mean...?Different times or what ?