-1

I have a script here that will give me an uptime but it is not the correct up time and I dont really know where the error is or if this is even the best way to do it but it is what I have. End game I want to set the uptime to a variable and if greater than x I will do a reboot or promt the user for a reboot and I will put this script in under the task scheduler. Either way here is what I have. Hope it is sufficient enough and I dont get scaled too bad over a stupid question.

@echo off 
setlocal ENABLEEXTENSIONS
call :Uptime d h n s
echo/Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
pause
goto :EOF

:Uptime days hours mins [secs]
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 & set "%1=%d%" & set "%2=%h%" & set "%3=%n%" & (if "%4" NEQ "" set "%4=%ss%") & goto :EOF
Leavii
  • 82
  • 2
  • 12

1 Answers1

1

Try a batch file leveraging powershell.

@Echo Off&SetLocal
For /F "UseBackQ Tokens=1-4" %%A In (
    `Powershell "$OS=GWmi Win32_OperatingSystem;$UP=(Get-Date)-"^
    "($OS.ConvertToDateTime($OS.LastBootUpTime));$DO='d='+$UP.Days+"^
    "' h='+$UP.Hours+' n='+$UP.Minutes+' s='+$UP.Seconds;Echo $DO"`) Do (
    Set "%%A"&Set "%%B"&Set "%%C"&Set "%%D")
Echo(Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
>Nul Timeout -1&Exit/B
Compo
  • 36,585
  • 5
  • 27
  • 39
  • As awesome as that worked pulling up everything about my PC it did not display the uptime. – Leavii Oct 06 '16 at 13:50
  • Please make sure you have copied the 8 lines of code correctly, and pasted them into a new .cmd file. (taking special care with having the correct number of lines within the for loop parentheses). It should not 'pull up' anything about your PC, only output a single line. – Compo Oct 06 '16 at 14:23
  • Yes it is just 8 lines and every parentheses is exactly how you have it there is not a character out of place. Is there anything I need to do in powershell? When this is ran it tells me my username, domain, all about my processor and my PC brand, platform etc... – Leavii Oct 06 '16 at 14:33
  • That sounds like you are getting an output of all of the information that would come from $OS which is technically `GWmi Win32_OperatingSystem`. The fact you are getting your reported output shows that you have no issues with Powershell. Your issue is most certainly with the code you have in your file. I pasted it directly from a working script I use in my scripts directory. – Compo Oct 06 '16 at 15:03
  • Wish I could add a snip of how I have it pasted it is exactly how you have it here. I am pretty oblivious at times but to just highlight something and hit CTRL+C then CTRL+V I think that is pretty cut and dry. – Leavii Oct 06 '16 at 15:26
  • If I knew how to do Ctrl+K in this comment window Id post what I have here but there is my obliviousness at work. – Leavii Oct 06 '16 at 15:27
  • As verification I copied and pasted the code into a new notepad created cmd file on a Windows 10 Home OS which had never ran powershell. The expected uptime output came, (although admittedly took a little time). As the code has worked directly prior to copying to this site and pasted directly from it, I cannot suggest anything else other than a difference at your end. – Compo Oct 07 '16 at 18:59
  • I have further removed the unnecessary **-C**ommand and repated the working code from my PC just to ensure that there weredefinitely no issues with the site formatting of the code. – Compo Oct 08 '16 at 14:17
  • So I tested it on another computer when I got back in today, and it works on that computer but not on mine. Not a huge deal cause I won't be running this on my PC. Any thoughts on why it doesn't work on mine would be nice if not no worries. Thanks again Compo you rock! – Leavii Oct 10 '16 at 21:54