{"id":65353,"date":"2007-03-09T01:13:00","date_gmt":"2007-03-09T01:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/09\/how-can-i-verify-the-system-time-on-a-remote-computer\/"},"modified":"2007-03-09T01:13:00","modified_gmt":"2007-03-09T01:13:00","slug":"how-can-i-verify-the-system-time-on-a-remote-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-verify-the-system-time-on-a-remote-computer\/","title":{"rendered":"How Can I Verify the System Time on a Remote Computer?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I verify the system time on a remote computer?<BR><BR>&#8212; MN<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MN. You know, recently the Scripting Guy who writes this column was reading about the \u201claw of attraction,\u201d 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:<\/P>\n<P>&#8220;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.&#8221;<\/P>\n<TABLE class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, there\u2019s always a catch: the law of attraction gives you whatever it is you are <I>thinking<\/I> about. No wonder the Scripting Guys never get what they want!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, to be honest, the Scripting Guy who writes this column tends to be a bit skeptical. The earth is round? Well, <I>maybe<\/I>. 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 \u2026.<\/P>\n<P>Interestingly enough, however, it was shortly after reading about the law of attraction that the Scripting Guy who writes this column actually started thinking: \u201cI don\u2019t know how to verify the system time on a remote computer,\u201d he thought. \u201cAnd I don\u2019t want to waste my afternoon trying to figure out how I <I>can<\/I> do it. I\u2019m thinking it would be nice if the universe would just give me a script that can verify the system time on a remote computer.\u201d<\/P>\n<P>Well, lo and behold:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_LocalTime&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strTime = objItem.Hour &amp; &#8220;:&#8221; &amp; objItem.Minute &amp; &#8220;:&#8221; &amp; objItem.Second\n    dtmTime = CDate(strTime)\n    Wscript.Echo FormatDateTime(dtmTime, vbFormatLongTime)\nNext\n<\/PRE>\n<P>Kind of spooky, isn\u2019t it?<\/P>\n<P>Best of all, this isn\u2019t a bad little script; in fact, it\u2019s even a bit fancier than anything the Scripting Guys were likely to come up with. Not that it\u2019s perfect, mind you; after all, it only runs on Windows XP and subsequent versions of Windows. (Apparently the universe doesn\u2019t realize that some people are still using Windows 2000.) But that\u2019s OK; before we call it a day we\u2019ll show you an alternate method for getting the system time that <I>will<\/I> work on Windows 2000. That\u2019s 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.<\/P>\n<P>Speaking of the universe, its script starts off by connecting to the WMI service on the local computer. But wait, you ask: didn\u2019t we want to get the system time on a <I>remote<\/I> computer? Don\u2019t 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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>After connecting to the WMI service we then use the following query to return all the instances of the Win32_LocalTime class:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_LocalTime&#8221;)\n<\/PRE>\n<P>Incidentally, this explains why the universe\u2019s script won\u2019t work on Windows 2000: the Win32_LocalTime class wasn\u2019t introduced until Windows XP. <\/P>\n<P>As you might expect, Win32_LocalTime is a \u201csingleton\u201d class; that means it returns only one item. (In general, a computer can only have one system time. We say \u201cin general\u201d because Scripting Guy Peter Costantini insists that on his home planet, located somewhere in the 35<SUP>th<\/SUP> dimension, computers <I>can<\/I> have more than one system time. Needless to say, we\u2019re a bit skeptical about <I>that<\/I>, too.) However, even though we only have one item the <B>ExecQuery<\/B> 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:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n<\/PRE>\n<P>And what is the universe going to have us do inside this loop? Well, for starters, we\u2019re going to grab the values of the <B>Hour<\/B>, <B>Minute<\/B>, and <B>Second<\/B> properties and string them together, storing the result in a variable named strTime:<\/P><PRE class=\"codeSample\">strTime = objItem.Hour &amp; &#8220;:&#8221; &amp; objItem.Minute &amp; &#8220;:&#8221; &amp; objItem.Second\n<\/PRE>\n<P>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\u2019t mind the 24-hour format then you\u2019re done. The universe, however, decided to add a couple extra lines of code in order to format this as a standard time.<\/P>\n<P>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<\/P><PRE class=\"codeSample\">14:33:16\n<\/PRE>\n<P>In order to convert this to a standard time format the universe first uses the <B>CDate<\/B> function to convert the character value 14:33:16 to a date-time value:<\/P><PRE class=\"codeSample\">dtmTime = CDate(strTime)\n<\/PRE>\n<P>After that the universe then uses the <B>FormatDateTime<\/B> function to echo the value of dtmTime using the time settings specified in your computer\u2019s Regional and Language Settings. (That\u2019s what the VBScript constant vbLongTme does.) On the Scripting Guys\u2019 test computer, that\u2019s going to look like this:<\/P><PRE class=\"codeSample\">2:33:16 PM\n<\/PRE>\n<P>That\u2019s a little nicer, especially if you aren\u2019t comfortable with the 24-hour clock.<\/P>\n<P>Well, it\u2019s nicer as long as you\u2019re not using Windows 2000; if you are, the preceding script won\u2019t help you much. But don\u2019t despair: Windows 2000 types can always use the <B>Win32_OperatingSystem<\/B> class and the <B>LocalDateTime<\/B> 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: <\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strTime = objItem.LocalDateTime\n    dtmTime = CDate(Mid (strTime, 9, 2) &amp; &#8220;:&#8221; &amp; Mid(strTime, 11, 2) &amp; &#8220;:&#8221; &amp; _\n        Mid(strTime, 13, 2))\n    dtmTime = CDate(dtmTime)\n    Wscript.Echo FormatDateTime(dtmTime, vbFormatLongTime)\nNext\n<\/PRE>\n<P>We won\u2019t discuss this script in any detail today, but you can learn more about the UTC time format by thumbing through the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_yakv.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P>\n<P>As for the Scripting Guys, we\u2019re assuming this is our last column; that\u2019s because we\u2019re thinking we\u2019d each like to have 10 million dollars so that we can all retire. If this law of attraction thing really works, well, tomorrow we\u2019ll all be lying on the beach drinking Mai Tais. It\u2019s been fun, but all good things must come to an end<\/P>\n<P>Well, unless some of you are thinking, \u201cGee, I hope there\u2019s a <I>Hey, Scripting Guy!<\/I> column tomorrow.\u201d How does the law of attraction deal with a situation where one person thinks X and another person thinks Y? We\u2019ll have to think about that one.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I verify the system time on a remote computer?&#8212; MN Hey, MN. You know, recently the Scripting Guy who writes this column was reading about the \u201claw of attraction,\u201d which is supposedly a natural law of the universe that, if you understand how to use it, can give you anything [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,3,5],"class_list":["post-65353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I verify the system time on a remote computer?&#8212; MN Hey, MN. You know, recently the Scripting Guy who writes this column was reading about the \u201claw of attraction,\u201d which is supposedly a natural law of the universe that, if you understand how to use it, can give you anything [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}