【问题标题】:How to get System Up time using a .bat? [closed]如何使用 .bat 获得系统启动时间? [关闭]
【发布时间】:2014-05-12 22:35:28
【问题描述】:

我需要使用批处理脚本从我网络中的所有计算机获取系统正常运行时间信息。

【问题讨论】:

    标签: batch-file uptime


    【解决方案1】:

    这是在独立机器上获得正常运行时间的一种方法。

    您可以使用psexec 在远程机器上运行它,或者可能更改 wmic 以从远程机器中提取信息。

    @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 ::::::::::::::::::::::::::::::::::::::::
    

    【讨论】:

    • 非常好,我花了几天时间写了超过 75 行代码,只完成了你脚本的一半。
    • 谢谢这个脚本工作得很好,我刚刚使用了 psexec,它完成了我需要的工作。干得好!
    • 优秀的脚本!但是没有必要使用 psexec,除非你真的想要。更改第 2 行和第 3 行中的以下部分以执行远程检查:“wmic OS Get”为“wmic ^/node^: OS Get”并将 替换为您要检查的计算机。只要您拥有工作站的管理员权限,就可以开始使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2014-09-10
    相关资源
    最近更新 更多