Jump to content

read cmd output


Recommended Posts

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

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

  • Developers

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 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

  • Developers

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

  • Developers

Something like this should work I guess:

#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

Function for CMD:

;~ #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

 

Regards,
 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...