Sign in to follow this  
Followers 0
Kate

Obtain boot time with AutoIT

13 posts in this topic

Hello,

Is there a way to use AutoIT to obtain the boot time of the computer? I'm looking to create a nag screen for users who don't reboot B)

Thanks in advance,

Kate

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

requires beta, but i think this is what your looking for (can be done remotely, just replace @ComputerName with the name of the computer)

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = @ComputerName

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

If IsObj($colItems) then
   For $objItem In $colItems
        $uptime = $objItem.SystemUpTime
        If $uptime > 60 Then 
            $uptime = $uptime / 60; convert the seconds to minutes
            If $uptime > 60 Then 
                $uptime = $uptime / 60; convert the minutes to hours
                If $uptime > 24 Then; convert hours to days
                    $uptime = Number(StringFormat("%.2f",$uptime / 24))
                    If $uptime > 1.0 Then; uptime more than 1 day
                        $Output = $Output & "SystemUpTime: " & $uptime & @CRLF
                        if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
                    EndIf
                EndIf
            EndIf
        EndIf
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Sometimes i think to myself... wow, this is what gafrost used to do..cool

maybe i am catching up to his skills...

then i see a post like this that shows me how much more gafrost has learned and i feel dumb .... all over again

nice script gary

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$ret = DllCall("kernel32.dll","long","GetTickCount")
If IsArray($ret) Then
    $uptime = $ret[0] / 1000; convert miliseconds to seconds
    If $uptime > 60 Then 
        $uptime = $uptime / 60; convert the seconds to minutes
        If $uptime > 60 Then 
            $uptime = $uptime / 60; convert the minutes to hours
            If $uptime > 24 Then; convert hours to days
                $uptime = Number(StringFormat("%.2f",$uptime / 24))
                If $uptime > 1.0 Then; uptime more than 1 day
                    MsgBox(0,"Uptime", $uptime)
                EndIf
            EndIf
        EndIf
    EndIf
EndIf

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

I had to move the msgbox to get an output since uptime was not more than 24 hours.

$ret = DllCall("kernel32.dll","long","GetTickCount")

; convert miliseconds to seconds
If IsArray($ret) Then $uptime = $ret[0] / 1000

; convert the seconds to minutes
If $uptime > 60 Then $uptime = $uptime / 60

; convert the minutes to hours
If $uptime > 60 Then $uptime = $uptime / 60

; convert hours to days
If $uptime > 24 Then $uptime = Number(StringFormat("%.2f",$uptime / 24))

MsgBox(0,"Uptime", $uptime)
What formatting did you have in mind if greater than one day?

...and thanks for the code...


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

I had to move the msgbox to get an output since uptime was not more than 24 hours.

$ret = DllCall("kernel32.dll","long","GetTickCount")

; convert miliseconds to seconds
If IsArray($ret) Then $uptime = $ret[0] / 1000

; convert the seconds to minutes
If $uptime > 60 Then $uptime = $uptime / 60

; convert the minutes to hours
If $uptime > 60 Then $uptime = $uptime / 60

; convert hours to days
If $uptime > 24 Then $uptime = Number(StringFormat("%.2f",$uptime / 24))

MsgBox(0,"Uptime", $uptime)
What formatting did you have in mind if greater than one day?

...and thanks for the code...

Leave your system on for more than a day and see what it returns, on my system right now it returned 3.93


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$ret = DllCall("kernel32.dll", "long", "GetTickCount")
If IsArray($ret) Then
   $type = "Seconds"
   $uptime = Number(StringFormat("%.2f", $ret[0] / 1000)); convert miliseconds to seconds
   If $uptime > 60 Then
      $type = "Minute(s)"
      $uptime = Number(StringFormat("%.2f", $uptime / 60)); convert the seconds to minutes
      If $uptime > 60 Then
         $type = "Hour(s)"
         $uptime = Number(StringFormat("%.2f", $uptime / 60)); convert the minutes to hours
         If $uptime > 24 Then; convert hours to days
            $type = "Day(s)"
            $uptime = Number(StringFormat("%.2f", $uptime / 24))
         EndIf
      EndIf
   EndIf
EndIf
MsgBox(0, "Uptime", $uptime & " " & $type)

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

incase you like another formating

$ret = DllCall("kernel32.dll", "long", "GetTickCount")
If IsArray($ret) Then
   $msec=StringRight("00" &  mod($ret[0],1000),3)
   $uptime=floor($ret[0]/1000)
   $sec=StringRight("00" & mod($uptime,60),2)
   If $uptime >= 60 Then
      $uptime= floor($uptime/60)
      $min=StringRight("00" & mod($uptime,60),2)
      If $uptime >= 60 Then
        $uptime=floor($uptime/60)
        $hour = StringRight("00" & mod($uptime,24),2)
         If $uptime >= 24 Then; convert hours to days
            $day=floor($uptime/24)
            $type=""
            if $day >1 then $type = "s"
            $type = " Day" & $type & " "
         EndIf
      EndIf
   EndIf
MsgBox(0, "Uptime", $day & $type &  $hour & ":" & $min &":"& $sec & '.'& $msec)
EndIf

Share this post


Link to post
Share on other sites

This is perfect!!! Thank you, all!!

Share this post


Link to post
Share on other sites

Just a shorter way to do the same thing.

$ret = DllCall("kernel32.dll","long","GetTickCount")

If IsArray($ret) Then
    $uptime = Round(($ret[0] / 3600000),1); convert miliseconds to hours
    MsgBox(0,"Uptime", $uptime,3)
EndIf

Share this post


Link to post
Share on other sites

Just a shorter way to do the same thing.

$ret = DllCall("kernel32.dll","long","GetTickCount")

If IsArray($ret) Then
    $uptime = Round(($ret[0] / 3600000),1); convert miliseconds to hours
    MsgBox(0,"Uptime", $uptime,3)
EndIf

A little help needed..

How would you use the dllcall function to check uptime on a remote machine?

Thanks

Share this post


Link to post
Share on other sites

On remote machines? I think WMI is the best way to go...requires of course active WMI and sufficient access rights on remote machine.

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

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

If IsObj($colItems) then
    For $objItem In $colItems
        $Output &= "SystemUpTime: " & $objItem.SystemUpTime ; in seconds
        if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
        $Output=""
    Next
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif

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