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

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

#16 Post by MKANET » 05 Jul 2012 16:22

Surprisingly, I couldn't find any system events that matched up exactly with wmic. So, this still remains a mystery.

However, at least, I did confirm that Microsoft's Uptime.exe utility comes from the pagefile last modified time stamp. Event ID = 6009 was one second different than the pagefile last modified time stamp; but not even close to what wmic shows.



aGerman wrote: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

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

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

#17 Post by aGerman » 05 Jul 2012 16:48

I can't confirm that the WMI differs from the event log (Win7 x86).

WMI:

Code: Select all

>wmic OS Get LastBootUpTime
LastBootUpTime
20120705181304.125599+120


System log (event ID 12):

Code: Select all

Das Betriebssystem wurde zur Systemzeit ‎2012‎-‎07‎-‎05T16:13:04.125599400Z gestartet

Of course there are exactly 2 hours offset between WMI and event log because of the time zone (UTC +60) and daylight time in summer (additional +60).

Regards
aGerman

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

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

#18 Post by MKANET » 05 Jul 2012 17:17

Win7 64bit. Not the same; even after correcting timezone offset.

wmic shows: 2012/06/30 12:22:56
event id 12 shows: 2012/06/30 19:23:01

Code: Select all

wmic OS Get LastBootUpTime

LastBootUpTime
20120630122256.633147-420



Code: Select all

Event 12
operating system started at system time ‎2012‎-‎06‎-‎30T19:23:01.109999300Z.

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

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

#19 Post by foxidrive » 05 Jul 2012 20:15

It looks like results from two different machines.

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

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

#20 Post by MKANET » 05 Jul 2012 21:15

No.. I only have one machine.
foxidrive wrote:It looks like results from two different machines.

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

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

#21 Post by foxidrive » 05 Jul 2012 21:29

Your first code showed you running it on remote machines.


You could have shown a remote machine boot time and executed the code on your local machine without realising it.

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

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

#22 Post by MKANET » 05 Jul 2012 21:50

Yeah my original code was meant for my work environment. I'm currently at home, with only one machine.

I wasn't able to find a matching timestamp in the system event logs. But there were ones that were closer than event id 12. Not sure why it would be different between our machines.

Image

foxidrive wrote:Your first code showed you running it on remote machines.


You could have shown a remote machine boot time and executed the code on your local machine without realising it.

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

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

#23 Post by foxidrive » 05 Jul 2012 22:52

It looks like you booted the machine 1/2 an hour before that and the likelihood is that you had booted it after that also and hadn't scrolled up in the log.

IOW it was finger trouble. ID-Ten-T error.

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

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

#24 Post by MKANET » 05 Jul 2012 23:04

Nope. Sorry, wrong again.

Image

foxidrive wrote:It looks like you booted the machine 1/2 an hour before that and the likelihood is that you had booted it after that also and hadn't scrolled up in the log.

IOW it was finger trouble. ID-Ten-T error.

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

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

#25 Post by foxidrive » 05 Jul 2012 23:08

What are you trying to show?

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

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

#26 Post by foxidrive » 05 Jul 2012 23:19

Yes, ID-ten-T error.

Take a good look here - and identify which of the 4 times you can see uses UTC time zone.

Image

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

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

#27 Post by MKANET » 05 Jul 2012 23:54

I think we might be talking about two different things. Either that, or maybe there's a language barrier? I was just explaining that "my machine" didn't have any event IDs that matched wmic. Especially event id 12.

You suggested I was using two different machine, which I proved I wasnt (first screenshot).
Then you suggested that there was another newer event 12 item which I hadn't scrolled up, which I proved it wasnt as well since it couldn't scroll any higher (second screenshot).

Who knows, there's probably something else you're trying to explain that somehow got lost in translation.

Also, I think there are probably much more respectable ways to say someone might be wrong than to repeatedly blurt ID-Ten-T error.

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

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

#28 Post by foxidrive » 06 Jul 2012 00:15

You missed the time zone - that is why they are different.

You are not very good with checking your questions or results, as you have shown today.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

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

#29 Post by siberia-man » 05 Aug 2019 23:48

Resurrecting the old thread. There is one more implementation of the initial idea.

Original code is here https://github.com/ildar-shaimordanov/c ... uptime.bat
I borrowed the idea how to convert YYYYMMDDhhmmss.SSSSSS+zzz to the unix timestamp here https://stackoverflow.com/a/11128674/3627676

Code: Select all

@echo off

:: ========================================================================

setlocal

set "uptime_boot="
set "uptime_curr="

for /f "tokens=1,* delims==" %%a in ( '
	wmic OS GET LastBootUpTime^,LocalDateTime /VALUE
' ) do if /i "%%~a" == "LastBootUpTime" (
	call :get_ts "%%~b" uptime_boot
) else if /i "%%~a" == "LocalDateTime" (
	call :get_ts "%%~b" uptime_curr
)

set /a "uptime_diff=uptime_curr-uptime_boot"

call :get_datetime diff
call :get_datetime curr

echo: %uptime_curr_hh%:%uptime_curr_mm%:%uptime_curr_ss% up %uptime_diff_dd% days, %uptime_diff_hh%:%uptime_diff_mm%

goto :EOF

:: ========================================================================

:get_datetime
set /a "uptime_%~1_ss=uptime_%~1 %% 60"
set /a "uptime_%~1_mm=uptime_%~1 / 60 %% 60"
set /a "uptime_%~1_hh=uptime_%~1 / 60 / 60 %% 24"
set /a "uptime_%~1_dd=uptime_%~1 / 60 / 60 / 24"

call set "uptime_%~1_ss=0%%uptime_%~1_ss%%"
call set "uptime_%~1_mm=0%%uptime_%~1_mm%%"
call set "uptime_%~1_hh=0%%uptime_%~1_hh%%"

call set "uptime_%~1_ss=%%uptime_%~1_ss:~-2%%"
call set "uptime_%~1_mm=%%uptime_%~1_mm:~-2%%"
call set "uptime_%~1_hh=%%uptime_%~1_hh:~-2%%"

goto :EOF

:: ========================================================================

:: Borowed from https://stackoverflow.com/a/11128674/3627676

:: Convert date/time from YYYYMMDDhhmmss.SSSSSS+zzz to Unix timestamp

:: 20190523222121.762088+180
:: 20190801180759.901000+180
::           1         2
:: 0123456789012345678901234

:: Get the timestamp in UTC
:get_ts_utc

:: Get the timestamp in local time
:get_ts
setlocal

set "ts=%~1"

set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100"
set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"

::set /a ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000)
set /a "ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100"
if "%~0" == ":get_ts_utc" set /a "ss-=%ts:~21,1%((1%ts:~22,3%*60)-60000)"

set /a ss+=dd*86400

endlocal & if "%~2" neq "" (set %~2=%ss%) else echo %ss%
goto :EOF

:: ========================================================================

:: EOF
Last edited by siberia-man on 16 Dec 2020 02:47, edited 2 times in total.

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

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

#30 Post by Eureka! » 07 Aug 2019 09:02

Why would anyone try to accomplish this in CMD these days, when there is PowerShell?

A hybrid soultion:

Code: Select all

@echo off
setlocal
echo.  Start = %TIME%
for /f "usebackq tokens=1,* delims=: " %%x in (`powershell.exe -nologo -noprofile -executionpolicy bypass "(get-date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime"`) do set %%x=%%y
echo.  Stop  = %TIME%
echo.
echo Uptime: %Days% days, %Hours% hours, %Minutes% minutes and %Seconds% seconds.
OUTPUT:

Code: Select all


T:\>uptime.cmd
  Start = 16:59:12,20
  Stop  = 16:59:13,41

Uptime: 0 days, 2 hours, 31 minutes and 0 seconds.

T:\>


EDIT:
Complete list of available variables:

Code: Select all

Days              : 0
Hours             : 3
Minutes           : 13
Seconds           : 59
Milliseconds      : 55
Ticks             : 116390556542
TotalDays         : 0,134711292293981
TotalHours        : 3,23307101505556
TotalMinutes      : 193,984260903333
TotalSeconds      : 11639,0556542
TotalMilliseconds : 11639055,6542

Post Reply