Jump to content

Recommended Posts

Posted (edited)

Neat.

I made a CMDLine version. It uses WMI so it can support Remote use.

edit:
     Revisted code and simplified WMI call
     2017-10-23 Added Ping test before WMI connection attempt and fail condition

Spoiler
#Region - Includes and Initilization
#AutoIt3Wrapper_Change2CUI=Y
#include <Date.au3>
#EndRegion

#Region - Command Line Parameter Execution/Logic
If @Compiled Then
    If $cmdline[0] Then ;Check for CMD Lime Parameters
        If StringInstr($cmdlineraw,"?") Then    ;Help
            ConsoleWrite(@CRLF & "Description:" & @CRLF & @TAB & "Displays system up time for local or remote machines." & @CRLF)
            ConsoleWrite(@CRLF & "Parameter List:" & @CRLF & @TAB & "/?" & @TAB & @TAB & @TAB & "Displays this help/usage." & @CRLF)
            ConsoleWrite(@CRLF & "Examples:" & @CRLF & @TAB & @ScriptName & @CRLF)
            ConsoleWrite(@TAB & @ScriptName & " hostname1 hostname2 hostname3" & @CRLF)
            Exit
        EndIf
        For $iX = 1 To $cmdline[0]  ;
            ConsoleWrite(StringUpper($cmdline[$iX]) & " " & _Uptime($cmdline[$iX])&@CRLF)
        Next
    Else
        ConsoleWrite(_Uptime() & @CRLF)
    EndIf
Else
    MsgBox(0,"Uptime:",_Uptime())
EndIf
#EndRegion

#Region - Uptime Function (Main)
Func _Uptime($sComputer = @ComputerName)
    $iPing = Ping($sComputer,4000)
    If Not  $iPing Then Return "Failed to Reach"
    $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\cimv2")
    If Not IsObj($oWMIService) Then Return "Failed to Connect"

    $oColOperatingSystems = $oWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
    For $oOS in $oColOperatingSystems
        Local $sBootup = $oOS.LastBootUpTime
        Local $sLastBootupTime = __WMIDateStringToDate($sBootup)
    Next

    $oColOperatingSystems = 0
    $oWMIService = 0

    Local $sUpTime = __FormatUptime($sLastBootupTime)
    Return "Last Boot: " & $sLastBootupTime & @CRLF & "Uptime: " & $sUpTime
EndFunc
#EndRegion

#Region - Internal Functions
Func __FormatUptime($sLastBootupTime)
    Local $aDateDiffs=[[0,24," day"],[0,60," hour"],[0,60," minute"],[0,1," second"]]
    Local $aDateDiff = StringSplit("D|h|n|s","|")
    Local $sNow = _NowCalc()
    For $sDiff = 1 to $aDateDiff[0]
        $aDateDiffs[$sDiff-1][0] = _DateDiff($aDateDiff[$sDiff], $sLastBootUpTime, $sNow)
    Next
    Local $iUbound = UBound($aDateDiffs)-1
    For $iX = $iUbound To 0 Step -1
        If $iX > 0 Then
            If $aDateDiffs[$iX-1][0] > 0 Then $aDateDiffs[$iX][0] = ($aDateDiffs[$iX][0]-($aDateDiffs[$iX-1][0]*$aDateDiffs[$iX-1][1]))
        EndIf
    Next
    Local $sUptime = ""
    For $iX = 0 To $iUbound
        If $aDateDiffs[$iX][0] > 0 Then
            $sUptime &= $aDateDiffs[$iX][0] & $aDateDiffs[$iX][2]
            If $aDateDiffs[$iX][0] > 1 Then $sUptime &= "s"
            If $iX < $iUbound Then $sUptime &= ", "
        EndIf
    Next
    Return $sUptime
EndFunc

Func __WMIDateStringToDate($sBootup)
    Return StringLeft($sBootup,4) & "/" & StringMid($sBootup, 5, 2) & "/" & StringMid($sBootup, 7, 2) & " " & StringMid($sBootup, 9, 2) & ":" & StringMid($sBootup, 11, 2) & ":" & StringMid($sBootup, 13, 2)
EndFunc
#EndRegion

 

Edited by spudw2k
source code update / cleanup
  • 4 months later...
Posted

Nice, but mines more colorful :blink:

Plus it calculation is "Last Boot". The other script I tried but gives another value if the system has been in standby. Last boot was 18 hours ago and last coming out of standby was 3.5 ours ago.

Different values, different needs, but I like the kernel way over the WMI way.

Thanks to the both of you anyway...

Posted

Plus it calculation is "Last Boot". The other script I tried but gives another value if the system has been in standby. Last boot was 18 hours ago and last coming out of standby was 3.5 ours ago.

Interesting observation, but I couldn't replicate the problem by putting my machine in standby.

I went with WMI for remote PCs; and for some reason the WMI call reflects the same time that "systeminfo" produces. Final's version is almost 1 minute behind, but that's not a big deal.

  • 2 weeks later...
Posted

Interesting observation, but I couldn't replicate the problem by putting my machine in standby.

I went with WMI for remote PCs; and for some reason the WMI call reflects the same time that "systeminfo" produces. Final's version is almost 1 minute behind, but that's not a big deal.

Blame that on Microsoft :blink:

  • 6 months later...
Posted

Simple script that keeps track of your computers uptime, also it doesn't use timers.

System Uptime.zip

This script seems very usefull, is it possible to place the uptime in a variable that I can use to write a reg key?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...