Sign in to follow this  
Followers 0
Deon

Get Remote Uptime as a different user

6 posts in this topic

I'm using the below code from an >old forum post, but I logon to my computer as a standard user, and I'll need to connect to the other computer as an Administrator (with a different username).

I can use RunAs for now, but I'd like a way to hard-code my credentials into the script (yes, I'm aware others can decompile it - no one else will be using the script), so I don't have to use RunAs everytime.

Here's the code at the moment:

#AutoIt3Wrapper_Change2CUI=Y
#include <Date.au3>

$strComputer = InputBox("Remote Uptime","What's the DNS?")
If $cmdline[0] Then
    If StringInstr($cmdlineraw,"?") Then
        MsgBox(0,"Remote Uptime",@CRLF & "Description:" & @CRLF & @TAB & "Displays system up time for local or remote machines." & @CRLF)
        MsgBox(0,"Remote Uptime",@CRLF & "Parameter List:" & @CRLF & @TAB & "/?" & @TAB & @TAB & @TAB & "Displays this help/usage." & @CRLF)
        MsgBox(0,"Remote Uptime",@CRLF & "Examples:" & @CRLF & @TAB & @ScriptName & @CRLF)
        MsgBox(0,"Remote Uptime",@TAB & @ScriptName & " hostname1 hostname2 hostname3" & @CRLF)
        Exit
    EndIf
    For $x = 1 To $cmdline[0]
        MsgBox(0,"Remote Uptime",StringUpper($cmdline[$x]) & " " & _Uptime($cmdline[$x])&@CRLF)
    Next
Else
    MsgBox(0,"Remote Uptime",_Uptime($strComputer) & @CRLF)
EndIf

Func _Uptime($strComputer)
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
    If Not IsObj($objWMIService) Then Return "Failed to connect... sorry about that."
    $colOperatingSystems = $objWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
    For $objOS in $colOperatingSystems
        $dtmBootup = $objOS.LastBootUpTime
        $dtmLastBootupTime = _WMIDateStringToDate($dtmBootup)
    Next

    Return _FormatUptime($dtmLastBootupTime)
EndFunc

Func _WMIDateStringToDate($dtmBootup)
    Return StringLeft($dtmBootup,4) & "/" & StringMid($dtmBootup, 5, 2) & "/" & StringMid($dtmBootup, 7, 2) & " " & StringMid($dtmBootup, 9, 2) & ":" & StringMid($dtmBootup, 11, 2) & ":" & StringMid($dtmBootup, 13, 2)
EndFunc

Func _FormatUptime($dtmLastBootupTime)
    Dim $arrdatediffs[4][3]=[[0,24," day"],[0,60," hour"],[0,60," minute"],[0,1," second"]]
    $datediff = StringSplit("D|h|n|s","|")
    $tNow = _NowCalc()
    For $diff = 1 to $datediff[0]
        $arrdatediffs[$diff-1][0] = _DateDiff($datediff[$diff], $dtmLastBootUpTime, $tNow)
    Next
    $ubound = UBound($arrdatediffs)-1
    For $x = $ubound To 0 Step -1
        If $x > 0 Then
            If $arrdatediffs[$x-1][0] > 0 Then $arrdatediffs[$x][0] = ($arrdatediffs[$x][0]-($arrdatediffs[$x-1][0]*$arrdatediffs[$x-1][1]))
        EndIf
    Next
    $strUptime = ""
    For $x = 0 To $ubound
        If $arrdatediffs[$x][0] > 0 Then
            $strUptime &= $arrdatediffs[$x][0] & $arrdatediffs[$x][2]
            If $arrdatediffs[$x][0] > 1 Then $strUptime &= "s"
            If $x < $ubound Then $strUptime &= ", "
        EndIf
    Next
    Return $strUptime
EndFunc

Share this post


Link to post
Share on other sites



Hey,

maybe something like this:

$User = ""
$Pass = ""

If $CmdLine[0] = 0 Then 
   RunAs($User, "domain", $Pass, flag, @ScriptFullPath & " -switchuser")
   Exit
EndIf

If $CmdLine[1] <> "-switchuser" Then Exit

; Rest of script

Share this post


Link to post
Share on other sites

Can you try this and provide some feedback whether it works? ->

Br,

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

@UEZ

Is there a particular reason you used SystemUpTime from the Win32_PerfFormattedData_PerfOS_System class? 

I used LastBootUpTime from the Win32_OperatingSystem class in my script and I don't recall it being susceptible to the sleep issue.

I'm just curious.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

spudw2k,

there was no particular reason why I used SystemUpTime from the Win32_PerfFormattedData_PerfOS_System class. As far as I can remember this was the WMI class which I found first.

I will change it to LastBootUpTime, too when the result is more accurate.

Thanks for the hint!

Btw, did you test my script whether the login credential code works properly? I've not the environment to test it anymore.

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
Btw, did you test my script whether the login credential code works properly? I've not the environment to test it anymore.

 

Br,

UEZ

Nor do I.  Hopefully the OP gets an opportunity to test it.

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