Trying to build a Uptime.exe alternative via native windows

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Trying to build a Uptime.exe alternative via native windows

#1 Post by MKANET » 04 Jul 2012 22:12

I am curious if there is existing dos batch file code that can calculate how many days, hours, minutes, and seconds a remote machine has been up.

I can get the date and time a remote machine booted up.

Very fast method:

Code: Select all

wmic /node:"%remotemachine%" OS Get LastBootUpTime


Slow:

Code: Select all

systeminfo /s %remotemachine% | find "Boot Time"


I can calculate the number of days using jdate. However, I'm still stuck with converting to hours, minutes, and seconds.
jdatehttp://www.dostips.com/DtCodeCmdLib.php#jdate.

I was hoping there is a more advanced version of jdate floating around that handles calculating the hours, minutes and seconds in addition to the julian date. I vaguely remember seeing something like that; but can't find it now.

Or, better yet.. maybe there's already dos batch code out there that already does this using just dos batch without having to rely on uptime.exe

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Trying to build a Uptime.exe alternative via native wind

#2 Post by miskox » 04 Jul 2012 23:36

Code: Select all

Systeminfo


has this info. Though I think this command is not available in XP Home. Don't know about other versions.

Also, I have a batch file (which I can upload later) which calculates the uptime using this (if I remember correctly) command:

Code: Select all

net statistics workstation


But it is possible to reset these counters so this info might not be 100% reliable.

Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to build a Uptime.exe alternative via native wind

#3 Post by foxidrive » 05 Jul 2012 00:36

Code: Select all

@echo off
for /f "delims=" %%a in ('wmic OS Get LastBootUpTime ^| find "."') do set up=%%a
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a

set beg=%up:~0,4%-%up:~4,2%-%up:~6,2%T%up:~8,2%-%up:~10,2%-%up:~12,2%-%up:~15,3%
set end=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%T%dt:~8,2%-%dt:~10,2%-%dt:~12,2%-%dt:~15,3%


call :TimeDifference uptime %end% %beg%

echo %beg%
echo %end%
echo Uptime is: %uptime%


pause
goto :eof

:TimeDifference Return_Variable Start_Date_Time Stop_Date_Time
:: Version -0 2009-12-25 Frank P. Westlake
:: Calculate the difference in time between parameter 2 and parameter 3
:: and return the values in the variable named by parameter 1.
::
:: Parameters 2 and 3 are ISO8601 DATE/TIMEs of the format
::
::     YYYY-MM-DDThh-mm-ss
::
:: where '-' may be any of '/:-., '.
::
:: RETURN:
:: The variable named by %1 will be set with a string containing each of
:: the following values seperated by spaces:
::
::   DAYS HOURS MINUTES SECONDS MILLISECONDS
::
:: EXAMPLE: Call :TimeDifference diff "2009-12-01T 1:00:00.00" "2009-11-30T13:00:00.01"
::          Sets variable "DIFF=0 12 0 0 10"
SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=1-14 delims=T/:-., " %%a in ("%~2 %~3") Do (
  Set "h2=0%%d" & Set "h3=0%%k" & Set "n2=%%g00" & Set "n3=%%n00"
  Set /A "y2=%%a, m2=1%%b-100, d2=1%%c-100, h2=1!h2:~-2!-100, i2=1%%e-100, s2=1%%f-100, n2=1!n2:~0,3!-1000"
  Set /A "y3=%%h, m3=1%%i-100, d3=1%%j-100, h3=1!h3:~-2!-100, i3=1%%l-100, s3=1%%m-100, n3=1!n3:~0,3!-1000"
)
Set /A "t2=((h2*60+i2)*60+s2)*1000+n2, t3=((h3*60+i3)*60+s3)*1000+n3"
Set /A "a=(14-m2)/12, b=y2-a, j2=(153*(12*a+m2-3)+2)/5+d2+365*b+b/4-b/100+b/400"
Set /A "a=(14-m3)/12, b=y3-a, j3=(153*(12*a+m3-3)+2)/5+d3+365*b+b/4-b/100+b/400"
Set /A "d=j3-j2, t=t3-t2"
If %d% LEQ 0 (If %d% LSS 0 (Set /A "d=j2-j3, t=t2-t3") Else If %t% LSS 0 (Set /A "t=t2-t3"))
If %t% LSS 0 (Set /A "t+=(1000*60*60*24), d-=1")
Set /A "n=t %% 1000, t/=1000, s=t %% 60, t/=60, m=t %% 60, t/=60"
EndLocal & Set "%~1=%d% %t% %m% %s% %n%"
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::




Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trying to build a Uptime.exe alternative via native wind

#4 Post by Squashman » 05 Jul 2012 06:01

miskox wrote:

Code: Select all

Systeminfo


has this info. Though I think this command is not available in XP Home. Don't know about other versions.


I believe you are correct on that but here is the code.
H:\>systeminfo | find "System Up Time"
System Up Time: 3 Days, 0 Hours, 29 Minutes, 12 Seconds

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to build a Uptime.exe alternative via native wind

#5 Post by foxidrive » 05 Jul 2012 06:18

It has changed in Windows 7

Code: Select all

System Boot Time:          Thursday 05/07/2012, 14:26:57

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trying to build a Uptime.exe alternative via native wind

#6 Post by Squashman » 05 Jul 2012 06:27

foxidrive wrote:It has changed in Windows 7

Code: Select all

System Boot Time:          Thursday 05/07/2012, 14:26:57

I gotta wonder what the hell MS is thinking some days!
NT 4 they provided a uptime tool to download which for the most parts works on NT,2000 and XP.
Then they give you the ability to find the uptime in XP Pro only.
Now they change it in Windows Vista and 7.
:evil:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to build a Uptime.exe alternative via native wind

#7 Post by foxidrive » 05 Jul 2012 06:31

Maybe they don't want you to know how often Windows crashes. ;)

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trying to build a Uptime.exe alternative via native wind

#8 Post by Squashman » 05 Jul 2012 06:36

foxidrive wrote:Maybe they don't want you to know how often Windows crashes. ;)

When I was a Linux server administrator I used to use a server side include to display the servers uptime on the bottom of a webpage. It was rather easy to do and that code hasn't changed in forever!

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Trying to build a Uptime.exe alternative via native wind

#9 Post by MKANET » 05 Jul 2012 11:06

Thanks foxdrive.

The below time difference code is what I was looking for. I'll make sure I hang on to it. Thanks!

Code: Select all

:TimeDifference Return_Variable Start_Date_Time Stop_Date_Time
:: Version -0 2009-12-25 Frank P. Westlake
:: Calculate the difference in time between parameter 2 and parameter 3
:: and return the values in the variable named by parameter 1.
::
:: Parameters 2 and 3 are ISO8601 DATE/TIMEs of the format
::
::     YYYY-MM-DDThh-mm-ss
::
:: where '-' may be any of '/:-., '.
::
:: RETURN:
:: The variable named by %1 will be set with a string containing each of
:: the following values seperated by spaces:
::
::   DAYS HOURS MINUTES SECONDS MILLISECONDS
::
:: EXAMPLE: Call :TimeDifference diff "2009-12-01T 1:00:00.00" "2009-11-30T13:00:00.01"
::          Sets variable "DIFF=0 12 0 0 10"
SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=1-14 delims=T/:-., " %%a in ("%~2 %~3") Do (
  Set "h2=0%%d" & Set "h3=0%%k" & Set "n2=%%g00" & Set "n3=%%n00"
  Set /A "y2=%%a, m2=1%%b-100, d2=1%%c-100, h2=1!h2:~-2!-100, i2=1%%e-100, s2=1%%f-100, n2=1!n2:~0,3!-1000"
  Set /A "y3=%%h, m3=1%%i-100, d3=1%%j-100, h3=1!h3:~-2!-100, i3=1%%l-100, s3=1%%m-100, n3=1!n3:~0,3!-1000"
)
Set /A "t2=((h2*60+i2)*60+s2)*1000+n2, t3=((h3*60+i3)*60+s3)*1000+n3"
Set /A "a=(14-m2)/12, b=y2-a, j2=(153*(12*a+m2-3)+2)/5+d2+365*b+b/4-b/100+b/400"
Set /A "a=(14-m3)/12, b=y3-a, j3=(153*(12*a+m3-3)+2)/5+d3+365*b+b/4-b/100+b/400"
Set /A "d=j3-j2, t=t3-t2"
If %d% LEQ 0 (If %d% LSS 0 (Set /A "d=j2-j3, t=t2-t3") Else If %t% LSS 0 (Set /A "t=t2-t3"))
If %t% LSS 0 (Set /A "t+=(1000*60*60*24), d-=1")
Set /A "n=t %% 1000, t/=1000, s=t %% 60, t/=60, m=t %% 60, t/=60"
EndLocal & Set "%~1=%d% %t% %m% %s% %n%"
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::


miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Trying to build a Uptime.exe alternative via native wind

#10 Post by miskox » 05 Jul 2012 12:36

Here it is, if anyone wants it:

Code: Select all

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Uptime days hours mins [secs]
::
:: By:   Ritchie Lawrence, 2003-09-24. Version 1.0
::
:: Func: Obtains the number of days, hours, minutes and seconds of uptime.
::       For NT4/2000/XP/2003.
::
:: Args: %1 var to receive number of days of uptime (by ref)
::       %2 var to receive number of hours of uptime (by ref)
::       %3 var to receive number of minutes of uptime (by ref)
::       %4 var to receive number of seconds of uptime (optional, by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS & set "c=net statistics work"
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
  for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
    set %%a=%%d&set %%b=%%e&set %%c=%%f))
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
  set "hh=%%a" & set "nn=%%b" & set "ss=%%c")
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
set /a hh=100%hh%%%100,nn=100%nn%%%100,f=j*1440+hh*60+nn
for /f "tokens=3-8 delims=/:M " %%a in ('%c%^|findstr/b /c:"Stat"') do (
  set mm=%%a&set dd=%%b&set yy=%%c&set hh=%%d&set nn=%%e%%f)
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if {%nn:~2,1%} EQU {P} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {A} if "%hh%" EQU "12" set hh=00
if {%nn:~2,1%} GEQ {A} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,s=j*1440+hh*60+nn,n=f-s
set /a d=n/1440,n%%=1440,h=n/60,n%%=60
endlocal & echo/Uptime is: %d% days, %h% hours, %n% minutes, %ss% seconds. & goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Saso

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Trying to build a Uptime.exe alternative via native wind

#11 Post by Aacini » 05 Jul 2012 13:12

You may also use my StdTime and StdDate programs for Time-to-milliseconds/Date-to-JDN and opposite conversions.

BTW, what represent the other data shown by wmic?

Code: Select all

>wmic OS Get LastBootUpTime
LastBootUpTime
20120705113410.500000-300
YYYYMMDDHHMMSS.??????????

Antonio

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Trying to build a Uptime.exe alternative via native wind

#12 Post by MKANET » 05 Jul 2012 13:40

Hi Antonio, actually the whole point was to keep from having to depend on external exe's in this case. However that link may come in handy in the future. Thanks.

I also wondered the same thing. Not sure what that is after the decimal point.

Aacini wrote:You may also use my StdTime and StdDate programs for Time-to-milliseconds/Date-to-JDN and opposite conversions.

BTW, what represent the other data shown by wmic?

Code: Select all

>wmic OS Get LastBootUpTime
LastBootUpTime
20120705113410.500000-300
YYYYMMDDHHMMSS.??????????

Antonio

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trying to build a Uptime.exe alternative via native wind

#13 Post by aGerman » 05 Jul 2012 13:42

See
CIM_DATETIME

yyyymmddHHMMSS.mmmmmmsUUU

Code: Select all

yyyy    Four-digit year (0000 through 9999).
mm      Two-digit month (01 through 12).
dd      Two-digit day of the month (01 through 31).
HH      Two-digit hour of the day using the 24-hour clock (00 through 23).
MM      Two-digit minute in the hour (00 through 59).
SS      Two-digit number of seconds in the minute (00 through 59).
mmmmmm  Six-digit number of microseconds in the second (000000 through 999999).
s       Plus sign (+) or minus sign (-) to indicate a positive or negative
        offset from Coordinated Universal Times (UTC).
UUU     Three-digit offset indicating the number of minutes that the
        originating time zone deviates from UTC.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Trying to build a Uptime.exe alternative via native wind

#14 Post by MKANET » 05 Jul 2012 14:28

Does anyone know where the wmic OS Get LastBootUpTime command originally get's it's boot start time from?

I know that the Windows Uptime.exe app bases it's info from a timestamp on the system page file. I think that systeminfo | find "Boot Time" also comes from the pagefile timestamp. I know of cases where there is something wrong with the pagefile, Uptime.exe gets confused with the timestamps; and, reports something weird like an uptime of 1200 days at my work. This doesn't happen too often, but it does happen.

With the help from this thread, I can make a better Uptime program for my work that first relies on WMIC. If the number of days is 1200 or more , to revert a less accurate method (the one that miskox posted); based on net statistics workstations; which gets this info from when the workstation service last started up.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trying to build a Uptime.exe alternative via native wind

#15 Post by aGerman » 05 Jul 2012 15:12

I'm virtually certain it queries informations from the event logs.
Run "eventvwr.msc" and have a look at the Windows System events.
The log files are usually saved in %SystemRoot%\System32\Config but they are locked.

Regards
aGerman

Post Reply