Sign in to follow this  
Followers 0
Kondro

Server Uptime

4 posts in this topic

#1 ·  Posted (edited)

Hello, I need to add to an existing script the ability to query a list of servers for system uptime and output to a variable and or a txt file. any ideas? Each server is already a variable as Dim $Server1, etc.

Edited by Kondro

Share this post


Link to post
Share on other sites



Maybe this will work for you - Ping.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

If you want to know how long the server is up and running you could use WMI to query the server.

There is available which creates the necessary AutoIt code for you.

I'm not at my PC at the moment so I can't test but google tells me you have to search in "Win32_PerfFormattedData_PerfOS_System" for "SystemUptime"


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here my version (commandline version):

 

[b]#AutoIt3Wrapper_Icon=CMD_48x48.ico
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Description=Displays the uptime of a (remote) system
#AutoIt3Wrapper_Res_Fileversion=0.8.0.0
#AutoIt3Wrapper_Res_LegalCopyright=UEZ 2011-2014
#AutoIt3Wrapper_Res_Language=1033
;#AutoIt3Wrapper_Res_Field=Coded by|UEZ
;#AutoIt3Wrapper_Res_Field=Build|2014-09-30
;#AutoIt3Wrapper_Res_Field=Compile date|%longdate% %time%
;#AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer%
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/so
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UPX_Parameters=--best --lzma
#AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_stripped.au3"

#RequireAdmin
#NoTrayIcon
#include <Array.au3>
#include <Date.au3>

Opt("MustDeclareVars", 1)
Global Const $ver = "v0.80 Build 2014-09-30"
Global $ip = @ComputerName
If $CmdLine[0] > 0 Then
    If  $CmdLine[1] = "/?" Or  $CmdLine[1] = "/h" Or  $CmdLine[1] = "-?" &  $CmdLine[1] = "-h" Then Help()
    Global Const $sCmdline = _ArrayToString($CmdLine, "", 1)
    Global Const $sHost = StringRegExpReplace($sCmdline, "(?i)-s(.*?)\-.*|-s(.*?)\s*", "$1")
    Global Const $sUser = StringRegExpReplace($sCmdline, "(?i).+-u(.+?)\-.*", "$1")
    Global Const $sPwd = StringRegExpReplace($sCmdline, "(?i).*-p(.*?)", "$1")
;~  ConsoleWrite("*" & $sHost & "*" & @CRLF)
    If $sHost <> "" Then $ip = $sHost
EndIf

Global $aResult, $objWMILocator, $objWMIService, $ping, $colItems

Switch $ip
    Case @ComputerName, "127.0.0.1", "localhost", @IPAddress1, @IPAddress2, @IPAddress3, @IPAddress4
        If @OSBuild < 6000 Then
            $aResult = DllCall("kernel32.dll", "dword", "GetTickCount")
            If @error Or Not IsArray($aResult) Then Exit ConsoleWrite(@LF & "An error has occured - unable to get uptime!" & @LF & @LF)
        Else
            $aResult = DllCall("kernel32.dll", "uint64", "GetTickCount64") ;only available on Vista+ os!
            If @error Or Not IsArray($aResult) Then Exit ConsoleWrite(@LF & "An error has occured - unable to get uptime!" & @LF & @LF)
        EndIf
        ConsolePrintUT(Floor($aResult[0] / 1000))
    Case Else
        $ping = Ping($ip, 500)
        If $ping Then
            $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
            $objWMIService = $objWMILocator.ConnectServer($ip, "\\.\root\cimv2", $sUser, $sPwd, "", "", 128)
            If @error Then Exit ConsoleWrite(@LF & "An error has occured - unable to get remote WMI connection!" & @LF & @LF)
            $colItems = $objWMIService.ExecQuery("SELECT SystemUpTime FROM Win32_PerfFormattedData_PerfOS_System", "WQL", 0x30)
            If IsObj($colItems) Then
                For $objItem In $colItems
                    ConsolePrintUT($objItem.SystemUpTime)
                Next
            Else
                Exit ConsoleWrite(@LF & "An error has occured - unable to get remote WMI data!" & @LF & @LF)
            EndIf
        Else
            Exit ConsoleWrite(@LF & "An error has occured - no ping reply from remote system " & $ip & "!" & @LF & @LF)
        EndIf

EndSwitch

Func ConsolePrintUT($iSeconds)
    ConsoleWrite(@LF & "Uptime on " & $ip & ": " & Sec_2_Time_Format($iSeconds) & @LF & @LF)
EndFunc

Func Sec_2_Time_Format($time)
    Local $days = 0
    Local $sec = Mod($time, 60)
    Local $min = Mod(Int($time / 60), 60)
    Local $hr = Int($time / 60 ^ 2)
    If $hr > 23 Then
        $days = Floor($hr / 24)
        $hr -= $days * 24
    EndIf
    Return StringFormat("%01id %02i:%02i:%02i // ", $days, $hr, $min, $sec) & _DateAdd("s", -$time, _NowCalc())
EndFunc   ;==>Sec_2_Time_Format

Func Help()
    ConsoleWrite(@CRLF & _
                            "Uptime " & $ver & " by UEZ" & @CRLF & @CRLF & _
                            "Usage: Uptime.exe -s [system] -u [user id] -p [password]" & @CRLF & @CRLF & @CRLF & _
                            "Displays the uptime of a system whereas system can be an ip address or" & @CRLF & _
                            "a hostname (must be reachable!)." & @CRLF & @CRLF & _
                            "Uptime output display format is:" & @CRLF & _
                            "d hh:mm:ss // yyyy/mm/dd hh:mm:ss as system start time " & @CRLF & @CRLF & _
                            "Default system name is the computer name where it has been started when no" & @CRLF & _
                            "parameter was given." & @CRLF & @CRLF)
    Exit
EndFunc[/b]

Remark: Uptime value from Win32_PerfFormattedData_PerfOS_System can differ compared to DllCall("kernel32.dll", "dword", "GetTickCount") when you put your computer to sleep!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

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