{"id":71493,"date":"2004-09-07T17:58:00","date_gmt":"2004-09-07T17:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/09\/07\/how-can-i-tell-if-a-server-has-rebooted\/"},"modified":"2004-09-07T17:58:00","modified_gmt":"2004-09-07T17:58:00","slug":"how-can-i-tell-if-a-server-has-rebooted","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-server-has-rebooted\/","title":{"rendered":"How Can I Tell if a Server has Rebooted?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! Is there any way to tell whether or not a server has rebooted?<\/p>\n<p>&#8212; MvdM<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, MvdM. We\u2019re assuming that you\u2019re performing a task and, at the end of the task, the computer is supposed to reboot. Needless to say, you want to know, \u201cWell, <i>did<\/i> the server reboot?\u201d How can you tell whether the machine restarted or not?<\/p>\n<p>The easiest way is to check the system uptime and see how long the computer has been running. Suppose your procedure was supposed to run at midnight, and when you come to work at 8:00 AM you want to know whether the computer restarted or not. Well, if it did, then the system uptime should be about 8 hours or so. If it is, then the odds are pretty good that the computer rebooted; if the system uptime is appreciably longer than 8 hours, then it probably <i>didn\u2019t<\/i> reboot.<\/p>\n<p>So how do you determine system uptime? That\u2019s easy: just use the WMI class Win32_OperatingSystem and check the value of the LastBootupTime property. Of course, there are two minor problems with that approach. First, the LastBootupTime property tells you when the computer last restarted; however, it doesn\u2019t tell you how long ago that was. Second, like all WMI date-time properties, LastBootupTime is reported in UTC format, meaning you get back a date that looks like this: 200409070130.000000+480. Yuck.<\/p>\n<p>But that\u2019s OK; just make sure your script converts the UTC date to a standard date-time format, and then use VBScript\u2019s DateDiff function to subtract the current date and time from the last bootup date and time; that will tell you how long the computer has been running since its last reboot. Here\u2019s a sample script that does all those things:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject _\n    (\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colOperatingSystems = objWMIService.ExecQuery _\n    (\"Select * from Win32_OperatingSystem\")\nFor Each objOS in colOperatingSystems\n    dtmBootup = objOS.LastBootUpTime\n    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)\n    dtmSystemUptime = DateDiff(\"h\", dtmLastBootUpTime, Now)\n    Wscript.Echo dtmSystemUptime\nNext\nFunction WMIDateStringToDate(dtmBootup)\n    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) &amp; \"\/\" &amp; _\n         Mid(dtmBootup, 7, 2) &amp; \"\/\" &amp; Left(dtmBootup, 4) _\n         &amp; \" \" &amp; Mid (dtmBootup, 9, 2) &amp; \":\" &amp; _\n         Mid(dtmBootup, 11, 2) &amp; \":\" &amp; Mid(dtmBootup, _\n         13, 2))\nEnd Function\n<\/pre>\n<p>Note two important points here. First, we have the function WMIDateStringToDate that converts the UTC date to a standard date-time value. We won\u2019t bother to explain all the code here, but all we\u2019re doing is teasing apart the UTC date-time values and then rearranging them in standard format. For example, with the UTC date 200409070130.000000+480 the first four digits represent the year. Thus our function takes the first four digits &#8211; Left(dtmBootup, 4) &#8211; and assigns that to the year portion of our standard date. We put the rest of our date-time value together using the same approach.<\/p>\n<p>Second, note the use of the DateDiff function to subtract the last bootup time from the current time. In this example, we\u2019re determining how many hours the server has been running; hence the parameter \u201ch\u201d, which reports time back in hours. Had we wanted to know the time in minutes, we would have used the \u201cm\u201d parameter. All in all pretty easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to tell whether or not a server has rebooted? &#8212; MvdM Hey, MvdM. We\u2019re assuming that you\u2019re performing a task and, at the end of the task, the computer is supposed to reboot. Needless to say, you want to know, \u201cWell, did the server reboot?\u201d How can you [&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":[41,31,3,5],"class_list":["post-71493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-monitoring","tag-operating-system","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there any way to tell whether or not a server has rebooted? &#8212; MvdM Hey, MvdM. We\u2019re assuming that you\u2019re performing a task and, at the end of the task, the computer is supposed to reboot. Needless to say, you want to know, \u201cWell, did the server reboot?\u201d How can you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71493","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=71493"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71493\/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=71493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}