Jump to content

Command woks in CMD but nothing happens when run AUTOIT - (Moved)


Recommended Posts

I am building a script that will connect remote to a system using PSEXEC and run a command to output to txt file. When I run in the AutoIt nothing happens, meaning I don't see the txt file getting create in the @TempDir\GetCert but if I copy the command that AutoIT just executed and paste in the CMD everything works fine. As you can see ConsoleWrite I tried everything, @ComSpec, I tried C:\Windows\System32\cmd.exe, etc. Again if I copy the entire command from the AutoIT output and paste on CMD it works just fine

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\Documents\Intel Security\Autoit\McAfee_icon.ico
#AutoIt3Wrapper_Outfile=GetCert (x86)1.exe
#AutoIt3Wrapper_Outfile_x64=GetCert (x64)1.exe
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
If FileExists (@TempDir & "\Psexec_loc.txt") Then
   $PsExec_temp = FileOpen (@TempDir & "\Psexec_loc.txt", 0)
   $SysInternal_Loc = FileReadLine ($PsExec_temp)
   FileClose ($PsExec_temp)
Else
   $SysInternal_Loc = "Full Path"
EndIf

#Region ### START Koda GUI section ### Form=c:\users\csilva\onedrive - mcafee\documents\psc\tools & docs\autoit\abbvie\getcert.kxf
$GetCert = GUICreate("GetCert", 432, 228, -1, -1)
$Button1 = GUICtrlCreateButton("Open", 328, 32, 83, 25)
$Input1 = GUICtrlCreateInput("", 16, 88, 393, 21)
$Input2 = GUICtrlCreateInput("Full Path", 16, 144, 393, 21)
$Label1 = GUICtrlCreateLabel("PsExec Location", 16, 8, 84, 17)
$Label2 = GUICtrlCreateLabel("Remote IP/Hostname", 16, 64, 107, 17)
$Label3 = GUICtrlCreateLabel("File Location", 16, 120, 64, 17)
$OK = GUICtrlCreateButton("OK", 336, 184, 75, 25)
$Input3 = GUICtrlCreateInput($SysInternal_Loc, 16, 32, 297, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

; Button1 is tied with Input3
Case $Button1
   $SysInternal_Loc = FileOpenDialog ('Open File', @ScriptDir, 'All (*.exe)', 1)

        If @error Then
            ; Display the error message.
            MsgBox(4096, "", "No file(s) were selected.")
         EndIf

GUICtrlSetData ($Input3, $SysInternal_Loc)

Case $OK
   $System_Loc = GUICtrlRead ($Input1)
   $FileLocation = GUICtrlRead ($Input2)
ExitLoop
    EndSwitch
 WEnd

GUIDelete ($GetCert)
$PsExec_temp = FileOpen (@TempDir & "\Psexec_loc.txt", 10)
FileWrite ($PsExec_temp, $SysInternal_Loc)
FileClose ($PsExec_temp)


;MsgBox (0, "", $SysInternal_Loc & @CRLF & $System_Loc & @CRLF & $FileLocation)

;$SysInternal_Loc = "Local SysInternal Folder Location"
$vtp_loc = "C:\Program Files\Common Files\McAfee\SystemCore\vtpinfo.exe"
;$System_Loc = "Remote Host Name or IP"
$ProcessName = GetProcessName ($FileLocation)


$sFile = @TempDir & "\GetCert"
If not DirCreate($sFile) Then

    MsgBox(16, "Error", "Failed to create the directory")
    Exit
EndIf


ConsoleWrite ("""C:\Windows\System32\cmd.exe""" & " /c """ & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """ > """ & @TempDir & "\GetCert" & "\" & $ProcessName & "-Cert.txt""")

;ConsoleWrite ($SysInternal_Loc & " \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """ > """ & @TempDir & "\GetCert" & "\" & $ProcessName & "-Cert.txt""")

;ConsoleWrite ("""" & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """ > """ & @TempDir & "\GetCert" & "\" & $ProcessName & "-Cert.txt""")

Sleep (5000)

Func GetProcessName ($File_Name)
$POS = StringInStr ($File_name, "\", 0 , -1)
$File_Name = StringTrimLeft ($File_Name, $POS)
$POS2 = StringInStr ($File_name, ".", 0, -1)
$File_Name = StringTrimRight ($File_Name, StringLen ($File_Name)-$POS2+1)
Return $File_Name
EndFunc


ShellExecute(@Tempdir & "\GetCert")

 

Edited by cesinha87
Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

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

I only see this line:

ConsoleWrite ("""C:\Windows\System32\cmd.exe""" & " /c """ & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """ > """ & @TempDir & "\GetCert" & "\" & $ProcessName & "-Cert.txt""")

This won't excecute it, only write it to the window in the bottom of Scite.

Maybe something like:

$sPSExecString="""C:\Windows\System32\cmd.exe""" & " /c """ & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """ > """ & @TempDir & "\GetCert" & "\" & $ProcessName & "-Cert.txt"""

ConsoleWrite ("Debug output:"&$sPSExecString&@CRLF)
Runwait($sPSExecString,"",@SW_HIDE)

You could also use run, combined with StdoutRead to read the input directly into AutoIT if you were going to further process the output in AutoIT, rather than relying on a text file.

Link to comment
Share on other sites

The code is working when run the command the problem I am not able to export the data to a text file. As you can see in the screenshot, AutoIT runs the PSexec and exports the cert successfully in the cmd but this data is not in variable. I tried in the code use > and does not create the file through AutoIT, but the output is correct

Now I am trying to use the STDOUTREAD and not working....at this point all I want is to export the data store in the variable $PSEXEC_RUN

 

$sPSExecString="""" & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """"

ConsoleWrite ($sPsExecString)
$PSEXEC_Run=Run($sPsExecString)


MsgBox(0, "CertResult", StdoutRead ($PSEXEC_Run))
;MsgBox(0, "CertResult", StderrRead ($PSEXEC_Run))

 

image.thumb.png.676f9771ce320489f0bc2154694bd73f.png

Link to comment
Share on other sites

I am very close to accomplish what I want, just need little more help. I can't figure out why my STDOUT READ is cutting a portion of the command. When I run the command locally in the CMD I get the text file on the right, the highlight portion is the portion I am missing from my Stdout Read in my AUTOIT. Looks like it's running PSEXEC in a loop and it's stopping from complete the first command. 

This how the command looks like now:

 

$sPSExecString="""" & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """"

ConsoleWrite ($sPsExecString)
$PSEXEC_Run=Run($sPsExecString,"", @SW_SHOW, 8)
ProcessWaitClose ($PSEXEC_Run,10)

$sOutput=""

 While 1
        $sOutput &= StdoutRead($PSEXEC_Run)
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
        MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    WEnd

$Stdout_cert=$sOutput

 $temp_filecreate=FileOpen (@TempDir & "\GetCert\" &  $ProcessName & "-Cert.txt", 10)
 FileWrite ($temp_filecreate, $Stdout_cert)
 FileClose ($temp_filecreate)



image.thumb.png.06c54e868d2c40236867c2cd8dcff153.png

Link to comment
Share on other sites

This is what I have so far and for some reason I am not able to pull the full data using AutoIT

$sPSExecString="""" & $SysInternal_Loc & """ \\" & $System_Loc & " """ & $vtp_loc & """ /GetCertChain """ & $FileLocation & """"


ConsoleWrite ($sPsExecString)
$PSEXEC_Run=Run($sPsExecString,"", @SW_SHOW, 8)
ProcessWaitClose ($PSEXEC_Run)


Local $sOutput="",$sRead

 While 1
   $sRead = StdoutRead($PSEXEC_Run)
   if $sRead = "" then ExitLoop
   $sOutput &= $sRead
WEnd

MsgBox (0,"",$sOutput)


$Stdout_cert=$sOutput
 $temp_filecreate=FileOpen (@TempDir & "\GetCert\" &  $ProcessName & "-Cert.txt", 10)
 FileWrite ($temp_filecreate, $Stdout_cert)
 FileClose ($temp_filecreate)

Copying the command from AutoIT output and paste in the CMD, this is what I get
image.thumb.png.ea3e36209a4b65bc5e6fff0a85d56af8.png

image.thumb.png.dea553001692829a2a2dd9af0fbdd3d6.png

 

That's not what I see in the MSGBOX or the txt that AutoIT creates

image.thumb.png.5d2842b763434e27906a86a345565bc6.png


 

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