Sign in to follow this  
Followers 0
Stretch

System up Time

8 posts in this topic

Im trying to get some code together to show how long a system has been up for...

So far have this -

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
;$Output = $Output & "Computer: " & $strComputer  & @CRLF
;$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      
      $Output = $Output & WMIDateStringToDate($objItem.LastBootUpTime) & @CRLF
     
     ;if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
     ;$Output=""
   Next
Else
  ;Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_OperatingSystem" )
Endif


Func WMIDateStringToDate($dtmDate)

        Return (StringLeft($dtmDate, 4) & "/" & _
    StringMid($dtmDate, 5, 2) & "/" & StringMid($dtmDate,7,2 ) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
    
    
EndFunc

;============================================================================================
$now=@CRLF & @YEAR&"/" & @MON&"/"  &@MDAY &" "&@HOUR &":" &@MIN &":" &@SEC

MsgBox(0,"",_DateDiff('h',$Output,$now))

Which gives me 18 at the moment.

I want someting like - System on for Xdays, Xhours and Xmins.

Any ideas?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I would make a program with a simple timer, add it to your startup folder, and with that timer parse the output time (in milliseconds) to read out in days, hours, minutes, seconds, etc.

$begin = TimerInit() ;begins timer
HotKeySet("+!t", "_Time") ; sets Shift-Alt-D as hetkey to show time

Func _Time()
     $dif = TimerDiff($begin)
     MsgBox(0, "", "It has been " & $dif & " milliseconds since the computer booted up.")
EndFunc

I honestly dont know how to parse milliseconds to show days, hours, and minutes at the same time. I could make it show how many days but not be able to turn the fraction of the day into hours and minutes. a more advanced user can help you with that. :whistle:

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Share this post


Link to post
Share on other sites

Try this:

#include <date.au3>
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
;$Output = $Output & "Computer: " & $strComputer  & @CRLF
;$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      
      $Output = $Output & WMIDateStringToDate($objItem.LastBootUpTime) & @CRLF
    
     ;if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
     ;$Output=""
   Next
Else
  ;Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_OperatingSystem" )
Endif


Func WMIDateStringToDate($dtmDate)

        Return (StringLeft($dtmDate, 4) & "/" & _
    StringMid($dtmDate, 5, 2) & "/" & StringMid($dtmDate,7,2 ) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
    
    
EndFunc

;============================================================================================
$now=@CRLF & @YEAR&"/" & @MON&"/"  &@MDAY &" "&@HOUR &":" &@MIN &":" &@SEC

;MsgBox(0,"",_DateDiff('h',$Output,$now))
; Added the lines below
$uptime = _DateDiff('s',$Output,$now)
$daysUp = Int($uptime/(60 * 60 * 24))
$remainder = Mod($uptime, 60 * 60 * 24)
$hoursUp = Int($remainder/(60 * 60))
$remainder = Mod($remainder, 60 * 60 )
$minUp = Int($remainder/60)
$secUp = Mod($remainder, 60)
MsgBox(0, "Uptime", "Your system has been up for:" & @LF & "  " & $daysUp & " days, " _
    & $hoursUp & " hrs, " & $minUp & " mins and " & $secUp & " secs.")

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

Perhaps this will work for you. You can ask kernel32.dll how long the system has been up, which may provide better information than using TimerInit().

#Include <Date.au3>

$aTSB = DllCall ("kernel32.dll", "long", "GetTickCount")
$ticksSinceBoot = $aTSB[0]

dim $iHours, $iMins, $iSecs
_TicksToTime ( $ticksSinceBoot, $iHours, $iMins, $iSecs )

$iDays = int($iHours / 24)
$iHours = $iHours - ($iDays * 24)

msgbox(0,"System Uptime","Days: "&$iDays& ", Hours: "&$iHours &", Minutes: "&$iMins & ", Seconds: "&$iSecs )

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

Thanks, Worked like a dream!

mark.

Share this post


Link to post
Share on other sites

Hi all,

i tried this code and it works well on my workstation. But, in my need, it tried to adapt it to call the uptime from a server and it don't works anyway.

By the past i realized some connexions to remote servers and workstations, but not with this code.

Do someone have any idea about it ?

Thks.

Share this post


Link to post
Share on other sites

Hi all,

I've tried an other way for realize the thing and i've found this code that works well.

Some of the text is in French, but that's not a problem because it's only text.

I use some INI files and encryption too.

#Include <date.au3>
#include <file.au3>
#Include <GUIconstants.au3>
$replog = IniRead("cfgconst.ini", "replog", "key", "NotFound")
$adminlog = IniRead("cfgconst.ini", "adminlog", "key", "NotFound")
If FileExists("cfgconst.ini") Then
;on fait rien
Else
    MsgBox(64,"Création du fichier de référence", "Attention !" & @CRLF & @CRLF & "Vous allez créer le fichier de référence des constantes de configuration" & @CRLF & @CRLF & "En cas de problème, vous pouvez modifier le fichier cfgconst.ini")
    $usercfg=InputBox("Nom de l'utilisateur/admin","Veuillez entrer le nom de l'utilisateur ayant accès aux machines : ","Administrateur")
    IniWrite("cfgconst.ini","User","Key",$usercfg)
    $nbservers=InputBox("Nombre de serveurs","Veuillez indiquer le nombre de serveurs : " & @CRLF & @CRLF & "(Attention ! Ayant le même mot de passe pour le même utilisateur)","")
    IniWrite("cfgconst.ini","NbServeurs","Key",$nbservers)
EndIf
$user = IniRead("cfgconst.ini", "User", "key", "NotFound")
$nb = IniRead("cfgconst.ini", "NbServeurs", "key", "NotFound")
If FileExists("serverslist.ini") Then
;on fait rien
Else
    MsgBox(64,"Création de la liste de serveurs", "Vous allez créer le fichier liste de référence des serveurs auxquels vous voulez accéder" & @CRLF & @CRLF & "En cas de problème, vous pouvez modifier le fichier serverlist.ini")
    MsgBox(48,"Consigne d'écriture", " - Le nom du serveur doit être indiqué tel qu'il puisse être résolu en DNS" & @CRLF & @CRLF & " - Vous pouvez entrer une adresse IP" & @CRLF & @CRLF & " - N'entrez aucun \ ou \\ avant ou après le nom de la machine " & @CRLF & @CRLF & " exemple : s4500120 " & @CRLF)
    For $d=1 to $nb
        $servername=InputBox("Nom du serveur","Veuillez entrer le nom du serveur " & $d & " sur " & $nb,"")
        IniWrite("serverslist.ini","Serveur" & $d,"Key", "\\" & $servername & "\" & @CRLF)
    Next
EndIf
If FileExists("cryptage.ini") Then
    $PASSKEY = "PASSWORD"
    $M_PASSWORD =IniRead("cryptage.ini","program","password","")
    $M_PASSWORD = Crypt($PASSKEY,Hex2Str($M_PASSWORD))
Else
    MsgBox(48,"Avertissement mot de passe", "Attention !" & @CRLF & @CRLF & "Vous allez créer le mot de passe de voter utilisateur : " & $user)
    $PASSKEY = "PASSWORD"
    $PASSWORD=InputBox("Mot de passe de " & $user,"Tapez le mot de passe à enregistrer : " & @CRLF & @CRLF & "(Il vous sera demandé de le confirmer mais ne sera plus demandé au prochain lancement)","","*")
    $TEMP = Str2Hex(Crypt($PASSKEY,$PASSWORD))
    IniWrite("cryptage.ini","program","password",$TEMP)
    $PASSWORD=InputBox("Vérification","Tapez le mot de passe pour vérification avec la version enregistrée : ","","*")
    $M_PASSWORD =IniRead("cryptage.ini","program","password","")
    $M_PASSWORD = Crypt($PASSKEY,Hex2Str($M_PASSWORD))
    If $M_PASSWORD == $PASSWORD Then
        MsgBox(0,'Ok', "Les mots de passe correspondent.")
    Else
        MsgBox(16,'Houlala !!!', "Les mots de passe correspondent pas !!!")
        FileDelete("cryptage.ini")
        Exit
    EndIf
EndIf
Func Crypt($PASS, $STRG)
    $A = 1
    $N_STRG = ""
    For $I = 1 To StringLen($STRG)
        $B = Asc(StringMid($PASS, $A, 1))
        $A = $A + 1
        If $A > StringLen($PASS) Then $A = 1
            $N_STRG = $N_STRG & Chr(BitXOR(Asc(StringMid($STRG, $I, 1)),$B))
    Next
    Return $N_STRG
EndFunc
Func Str2Hex($STRG)
    $H = ""
    For $I = 1 To StringLen($STRG)
        $H = $H & String(Hex(Asc(StringMid($STRG, $I, 1)),2))
    Next
    Return $H
EndFunc
Func Hex2Str($H)
    $STRG = ""
    For $I = 1 To StringLen($H) Step 2
        $STRG = $STRG & Chr(Dec(StringMid($H, $I, 2)))
    Next
Return $STRG
EndFunc
$log = FileOpen($replog & $adminlog, 2)
FileWrite($log, "Serveur        " & "Date Reboot            " & "Heures     " & "Heure locale" & @CRLF)
For $i=1 to $nb
    $serv = IniRead("serverslist.ini", "Serveur" & $i, "key", "NotFound")
    $repserv = StringReplace($serv, "\", "")
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = $repserv
    $Output = ""
    $Output = $Output & "Ordinateur : " & $strComputer & @CRLF
    FileWrite($log, $strComputer & "    ")
    $Output = $Output & "================================" & @CRLF
    $wmiLocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $user, $M_PASSWORD)
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
       For $objItem In $colItems
          $LastBootUp = WMIDateStringToDate($objItem.LastBootUpTime)
          $LocalDateTime = WMIDateStringToDate($objItem.LocalDateTime)
          $Output = $Output & "Dernier Reboot : " & $LastBootUp & @CRLF
          FileWrite($log, $LastBootUp & "       ")
          $Output = $Output & "================================" & @CRLF
         ; $Output = $Output & "En années : " & _DateDiff("Y", $LastBootUp, $LocalDateTime) & @CRLF
         ; $Output = $Output & "En mois : " & _DateDiff("M", $LastBootUp, $LocalDateTime) & @CRLF
         ; $Output = $Output & "En semaines : " & _DateDiff("W", $LastBootUp, $LocalDateTime) & @CRLF
          $Output = $Output & "En jours : " & _DateDiff("D", $LastBootUp, $LocalDateTime) & @CRLF
          $Output = $Output & "En heures : " & _DateDiff("h", $LastBootUp, $LocalDateTime) & @CRLF
          FileWrite($log, _DateDiff("h", $LastBootUp, $LocalDateTime) & "       ")
          $Output = $Output & "En Minutes : " & _DateDiff("n", $LastBootUp, $LocalDateTime) & @CRLF
         ; $Output = $Output & "En Secondes : " & _DateDiff("s", $LastBootUp, $LocalDateTime) & @CRLF & @CRLF
          $Output = $Output & "----------------------------------------------------------------" & @CRLF
          $Output = $Output & "Heure locale de " & $strComputer & " : " & $LocalDateTime & @CRLF
          FileWrite($log, $LocalDateTime & @CRLF)
         ;If MsgBox(1, "Extraction WMI - Dernier reboot machine", $Output) = 2 Then ExitLoop
        $Output = ""
       Next
    Else
       MsgBox(0, "WMI Output", "Pas d'objets WMI trouvés pour cette classe : " & "Win32_OperatingSystem")
    EndIf
Next
FileClose($log)

Func WMIDateStringToDate($dtmDate)  
    Return (StringMid($dtmDate, 1, 4) & "/" & StringMid($dtmDate, 5, 2) & "/" & StringMid($dtmDate, 7, 2) & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate, 13, 2))
EndFunc

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0