Deon Posted March 27, 2014 Share Posted March 27, 2014 Apologies for making this a bit of a complicated one... I'm looking for a remote uptime script. I've found a few, and something like one of the below would probably do... however I need to be able to run the script as another user. For security, we don't use our domain admin accounts on a day-to-day basis, only when we need to run scripts that require domain admin permissions. Eg, I'm logged in all day as $regularuser and use $domainadmin when I log into a server... $regularuser doesn't have admin rights on the servers, so this code must be executed as $domainadmin. The script will be placed in a network share, so I can't use RunAs in Windows, because when the session is created for the RunAs user ($domainadmin), the network share will not be mapped and the script won't be found. Any ideas? expandcollapse popup#AutoIt3Wrapper_Change2CUI=Y #include <Date.au3> $strComputer = "." If $cmdline[0] Then If StringInstr($cmdlineraw,"?") Then 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 $x = 1 To $cmdline[0] ConsoleWrite(StringUpper($cmdline[$x]) & " " & _Uptime($cmdline[$x])&@CRLF) Next Else ConsoleWrite(_Uptime($strComputer) & @CRLF) EndIf Func _Uptime($strComputer) $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") If Not IsObj($objWMIService) Then Return "Failed to Connect" $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 or $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" Local $strComputer = InputBox("Which computer?","Enter Computer name") $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 Link to comment Share on other sites More sharing options...
orbs Posted March 28, 2014 Share Posted March 28, 2014 hello Deon, welcome to AutoIt and to the forum! The script will be placed in a network share, so I can't use RunAs in Windows, because when the session is created for the RunAs user ($domainadmin), the network share will not be mapped and the script won't be found. Any ideas? you can use RunAs, but do not use a drive letter, use the UNC path instead. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now