{"id":69263,"date":"2005-08-02T13:38:00","date_gmt":"2005-08-02T13:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/02\/how-can-i-determine-the-uptime-for-a-server\/"},"modified":"2005-08-02T13:38:00","modified_gmt":"2005-08-02T13:38:00","slug":"how-can-i-determine-the-uptime-for-a-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-uptime-for-a-server\/","title":{"rendered":"How Can I Determine the Uptime for a Server?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I determine the uptime for a server?<BR><BR>&#8212; LF<\/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, LF. This is an easy one. After all, we\u2019re assuming you\u2019re talking about a Windows server, and Windows servers never go down, right? Therefore, the uptime must be forever. Problem solved.<\/P>\n<P>Well, OK, we suppose that maybe a Windows server <I>could<\/I> go down (probably because a non-Windows user snuck in and pulled the plug or something). If you need to know the system uptime on either a Windows XP or a Windows Server 2003 computer you can use a script like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colOperatingSystems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PerfFormattedData_PerfOS_System&#8221;)<\/p>\n<p>For Each objOS in colOperatingSystems\n    intSystemUptime = Int(objOS.SystemUpTime \/ 60)\n    Wscript.Echo intSystemUptime &amp; &#8221; minutes&#8221;\nNext\n<\/PRE>\n<P>As you can see, we connect to the WMI service and then use the <B>ExecQuery<\/B> method to retrieve all instances of the <B>Win32_PerfFormattedData_PerfOS_System<\/B> class. (This, by the way, explains why the script runs only on Windows XP or Windows Server 2003: those are the only two versions of Windows that include the Win32_PerfFormattedData_PerfOS_System class.) <\/P>\n<P>Win32_PerfFormattedData_PerfOS_System contains a number of performance counters related to the operating system, including <B>SystemUpTime<\/B>, which tells you how many second the machine has been running. We use this line of code to grab the value of SystemUpTime, divide it by 60 (thus giving us the uptime in minutes rather than seconds), and then convert it to an integer, stripping away anything after the decimal point:<\/P><PRE class=\"codeSample\">intSystemUptime = Int(objOS.SystemUpTime \/ 60)\n<\/PRE>\n<P>Yes, that <I>is<\/I> an awful lot for one little line of code to do, isn\u2019t it? We then echo the value of the variable intSystemUptime and we\u2019re done.<\/P>\n<P>Well, unless you\u2019re running Windows 2000, that is. Like we said, the preceding script won\u2019t run on Windows 2000; Windows 2000 has never even <I>heard<\/I> of the Win32_PerfFormattedData_PerfOS_System class. Fortunately there <I>is<\/I> a workaround; it\u2019s a bit clumsy, but it\u2019s a workaround nevertheless:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colOperatingSystems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objOS in colOperatingSystems\n    dtmBootup = objOS.LastBootUpTime\n    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)\n    dtmSystemUptime = DateDiff(&#8220;n&#8221;, dtmLastBootUpTime, Now)\n    Wscript.Echo dtmSystemUptime &amp; &#8221; minutes&#8221;\nNext<\/p>\n<p>Function WMIDateStringToDate(dtmBootup)\n    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) &amp; &#8220;\/&#8221; &amp; _\n        Mid(dtmBootup, 7, 2) &amp; &#8220;\/&#8221; &amp; Left(dtmBootup, 4) _\n            &amp; &#8221; &#8221; &amp; Mid (dtmBootup, 9, 2) &amp; &#8220;:&#8221; &amp; _\n                Mid(dtmBootup, 11, 2) &amp; &#8220;:&#8221; &amp; Mid(dtmBootup,13, 2))\nEnd Function\n<\/PRE>\n<P>In this script we connect to the WMI service and then query the <B>Win32_OperatingSystem<\/B> class; that\u2019s because Win32_OperatingSystem includes a property &#8211; LastBootUpTime &#8211; that can tell you the last time the computer started. That\u2019s useful: if we subtract LastBootUpTime from the current time we\u2019ll know &#8211; that\u2019s right &#8211; how long the computer has been running.<\/P>\n<P>The only tricky part is the fact that, like all WMI dates and times, LastBootUpTime is stored in the UTC (Universal Time Coordinate) format. That means you\u2019re going to get back a value that looks like this:<\/P><PRE class=\"codeSample\">20050726071152.500000-420\n<\/PRE>\n<P>Before you try, don\u2019t bother subtracting that from the current date and time; it won\u2019t work. Instead, we need to convert this into something that looks more like a real date and time; that\u2019s why this line of code calls a function we wrote (WMIDateStringToDate) that converts a UTC datetime value into a regular old date-time value:<\/P><PRE class=\"codeSample\">dtmLastBootupTime = WMIDateStringToDate(dtmBootup)\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EDE\" 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>. We won\u2019t explain how the function works. If you\u2019re interested in that information, see <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_yakv.mspx\" target=\"_blank\"><B>this portion<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After making the conversion we use the <B>DateDiff<\/B> function to subtract the last bootup time from the current time:<\/P><PRE class=\"codeSample\">dtmSystemUptime = DateDiff(&#8220;n&#8221;, dtmLastBootUpTime, Now)\n<\/PRE>\n<P>As you can see, we pass DateDiff three parameters: <\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201cn\u201d<\/B>, which means to report the time difference in minutes.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>dtmLastBootUpTime<\/B>, a variable holding the converted datetime value.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Now<\/B>, the current date and time.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>All we do then is echo back the system uptime in minutes.<\/P>\n<P>Like we said, we can\u2019t imagine why you\u2019d ever need a script like this for a Windows computer, but just in case \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the uptime for a server?&#8212; LF Hey, LF. This is an easy one. After all, we\u2019re assuming you\u2019re talking about a Windows server, and Windows servers never go down, right? Therefore, the uptime must be forever. Problem solved. Well, OK, we suppose that maybe a Windows server could [&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-69263","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! How can I determine the uptime for a server?&#8212; LF Hey, LF. This is an easy one. After all, we\u2019re assuming you\u2019re talking about a Windows server, and Windows servers never go down, right? Therefore, the uptime must be forever. Problem solved. Well, OK, we suppose that maybe a Windows server could [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69263","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=69263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69263\/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=69263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}