hendrikhe Posted August 17, 2017 Share Posted August 17, 2017 Hello AutoIt community, I need to read the username using the qwista command in CMD. I found so far the code below in this post: But my MsgBox gets me an empty username #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> Global $DOS, $Message = '' ;; added "= ''" for show only. #include <MsgBoxConstants.au3> Func _ConsoleUser() $pid=Run(@SystemDir&"\qwinsta","",@sw_hide,$STDERR_CHILD + $STDOUT_CHILD) ProcessWaitClose($pid) $output = StdoutRead($pid) ; $offset=StringinStr($output,"console") ; $username=StringStripWS(StringMid($output,$offset+18,20),3) $username=StringStripWS(StringMid($output,StringinStr($output,"console")+18,20),3) return $username EndFunc MsgBox(0, "username:", _ConsoleUser()) Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted August 18, 2017 Share Posted August 18, 2017 Good morning @hendrikhe Why do you need to use qwinsta instead of @UserName provided by AutoIt? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
hendrikhe Posted August 18, 2017 Author Share Posted August 18, 2017 I will run a script with a different user but I need the current logged in username. I am able to retrieve the current loggged in user with different methods on my computer and VMware but my company computer is driving me crazy. My last hope is qwinsta ! Link to comment Share on other sites More sharing options...
Developers Jos Posted August 18, 2017 Developers Share Posted August 18, 2017 How do you shell the script so it runs with that different userid? Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
hendrikhe Posted August 18, 2017 Author Share Posted August 18, 2017 Shift , right click: run as different user Link to comment Share on other sites More sharing options...
Developers Jos Posted August 18, 2017 Developers Share Posted August 18, 2017 (edited) Ok.. so this is always a manual start and typing in the userid&Password? You could just shell your script normally, prompt for this runas userid and password and then do a runas() and provide the current real user as a parameter on the commandline. Jos Edited August 18, 2017 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
hendrikhe Posted August 18, 2017 Author Share Posted August 18, 2017 UAC is disabled and I need the admin rights, RunAs will not work for me Link to comment Share on other sites More sharing options...
Developers Jos Posted August 18, 2017 Developers Share Posted August 18, 2017 Why would that not work? There are several way to do that too. Just build the script in the initial shelled script like I do in Autoit3Wrapper.au3 script for Func RunReqAdminDosCommand() (I assume you have the Full SciTE4AutoIt3 version installed) Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
hendrikhe Posted August 18, 2017 Author Share Posted August 18, 2017 Yes I have the Full SciTE4AutoIt3 version installed , but I am very noob with Autoit3Wrapper.au3 I am not sure how to implement RunReqAdminDosCommand() and RunAs, do you have me a short example? Link to comment Share on other sites More sharing options...
Developers Jos Posted August 18, 2017 Developers Share Posted August 18, 2017 Something like this should work I guess: expandcollapse popup#include <File.au3> ; Read the parameters to scheck whether the script is reshelled For $x = 1 To $CMDLINE[0] MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, @UserName) ;### Debug MSGBOX $T_Var = StringLower($CMDLINE[$x]) Select Case $T_Var = "/ReShell" AutoItWinSetTitle("My Reshelled AutoIt3") ; ---- Process the stuff that needs @Admin & RunAs ------ ; --- end process the stuff Exit EndSelect Next ; Main code MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, @UserName) ;### Debug MSGBOX ; Get the Userid & Password and reshell the same script $RunAsID = "testid" $RunAsPW = "testpw" $RunAsDomain = @ComputerName RunReqAdmin('RunasWait("'& $RunAsID & '","' & $RunAsDomain & '","' & $RunAsPW & '",0,''"' & @AutoItExe & '" "' & @ScriptFullPath & '" /ReShell /userid=' & @UserName & ''')') ; End Main code Func RunReqAdmin($Autoit3Command) Local $temp_Script = _TempFile(@TempDir, "~", ".au3") ; Start building the tempscriptfile FileWriteLine($temp_Script, '#NoTrayIcon') If Not IsAdmin() Then FileWriteLine($temp_Script, '#RequireAdmin') EndIf FileWriteLine($temp_Script, $Autoit3Command) $RC = RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $temp_Script & '"') ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ; give the runwait time to run the tempfile Sleep(1000) FileDelete($temp_Script) EndFunc ;==>RunReqAdmin Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Trong Posted August 18, 2017 Share Posted August 18, 2017 Function for CMD: expandcollapse popup;~ #RequireAdmin #include <File.au3> If @OSArch = "X64" And Not @AutoItX64 Then _Wow64FsRedirection(0) Local $output= _RunCmd_GetOutput("qwinsta") ;~ Local $output=_StreamCMD("ping 8.8.8.8") Local $username=StringStripWS(StringMid($output,StringinStr($output,"console")+18,20),3) MsgBox(0, "", $username) Func _RunCmd_GetOutput($sCommand) ConsoleWrite("+Execute: " & $sCommand & @CRLF) Local $sOutput = '', $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, '', @SW_HIDE, 0x6) Do $sOutput &= StdoutRead($iPID) Until @error Do $sOutput &= StderrRead($iPID) Until @error ConsoleWrite($sOutput&@CRLF) Return $sOutput EndFunc ;==>_RunCmd Func _StreamCMD($sCMD, $sCallBackFunction = Default, $WorkingDir = Default, $iStreamType = Default, $iShowFlag = Default, $iDelay = Default) If StringStripWS($sCMD, 8) = "" Then Return "" If $sCallBackFunction = Default Then $sCallBackFunction = "ConsoleWrite" ;~ If $WorkingDir = Default Then $WorkingDir = @SystemDir ;@WindowsDir & '\System32' If $WorkingDir = Default Then $WorkingDir = @WindowsDir & '\System32' If $iStreamType = Default Then $iStreamType = $STDERR_CHILD + $STDOUT_CHILD If $iShowFlag = Default Then $iShowFlag = False If $iDelay = Default Then $iDelay = 250 ConsoleWrite("! Execute: " & $sCMD & @CRLF) Local $sTMP = '', $sSTD = '', $sCOM = '"' & @WindowsDir & '\System32\cmd.exe"' & ' /c ' & $sCMD ;~ Local $sTMP = '', $sSTD = '', $sCOM = @ComSpec & ' /c ' & $sCMD Local $iWin = $iShowFlag ? @SW_SHOW : @SW_HIDE Local $iPID = Run($sCOM, $WorkingDir, $iWin, $iStreamType) While 1 $sTMP = StdoutRead($iPID, False, False) If @error Then ExitLoop If $sTMP <> "" Then $sTMP = StringReplace($sTMP, @CR & @CR, '') $sSTD &= $sTMP Call($sCallBackFunction,$sTMP) ;~ ConsoleWrite($sTMP) Sleep($iDelay) EndIf WEnd While 1 $sTMP = StderrRead($iPID, False, False) If @error Then ExitLoop If $sTMP <> "" Then $sTMP = StringReplace($sTMP, @CR & @CR, '') $sSTD &= $sTMP Call($sCallBackFunction,$sTMP) ;~ ConsoleWrite($sTMP) Sleep($iDelay) EndIf WEnd ;~ If $sSTD <> "" Then ConsoleWrite(@CRLF) Return SetError(@error, @extended, $sSTD) EndFunc ;==>_StreamCMD ; * -----:| Dao Van Trong - TRONG.WIN Func _Wow64FsRedirection($state) If @OSArch = "X64" Then If $state Then ;DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1) DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0) Else ;DllCall('kernel32.dll', 'boolean', 'Wow64EnableWow64FsRedirection', 'boolean', False) DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0) EndIf If @error Then Return SetError(1, 0, 0) Return 1 EndIf EndFunc ;==>_Wow64FsRedirection Marc and GoogleDude 2 Regards, 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