如何批量生产PC正常运行时间?

时间:2016-10-05 21:06:47

标签: batch-file

我这里有一个脚本会给我一个正常运行时间,但它不是正确的运行时间,我真的不知道错误在哪里,或者这是否是最好的方法,但它就是我拥有的。结束游戏我想将正常运行时间设置为变量,如果大于x,我将重新启动或促使用户重启,我将把这个脚本放在任务调度程序下。无论哪种方式都是我拥有的。希望它足够了,我不会因为一个愚蠢的问题而变得太糟糕。

@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

1 个答案:

答案 0 :(得分:1)

尝试使用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