cesinha87 Posted May 7, 2019 Share Posted May 7, 2019 (edited) 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 expandcollapse popup#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 May 7, 2019 by cesinha87 Link to comment Share on other sites More sharing options...
Developers Jos Posted May 7, 2019 Developers Share Posted May 7, 2019 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 More sharing options...
TurionAltec Posted May 7, 2019 Share Posted May 7, 2019 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 More sharing options...
cesinha87 Posted May 7, 2019 Author Share Posted May 7, 2019 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)) Link to comment Share on other sites More sharing options...
Nine Posted May 7, 2019 Share Posted May 7, 2019 Try this : $PSEXEC_Run=Run($sPsExecString,"", @SW_SHOW, $STDOUT_CHILD) ProcessWaitClose ($PSEXEC_Run) MsgBox(0, "CertResult", StdoutRead ($PSEXEC_Run)) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
cesinha87 Posted May 7, 2019 Author Share Posted May 7, 2019 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) Link to comment Share on other sites More sharing options...
Nine Posted May 7, 2019 Share Posted May 7, 2019 (edited) Your msgbox should be outside the loop... Edited May 7, 2019 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
cesinha87 Posted May 7, 2019 Author Share Posted May 7, 2019 Nine thank you for helping me....I tried that as well. No Lucky Link to comment Share on other sites More sharing options...
Nine Posted May 7, 2019 Share Posted May 7, 2019 Maybe this : Local $sOutput="",$sRead While 1 $sRead = StdoutRead($PSEXEC_Run) if $sRead = "" then ExitLoop $sOutput &= $sRead WEnd MsgBox (0,"",$sOutput) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
cesinha87 Posted May 7, 2019 Author Share Posted May 7, 2019 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 That's not what I see in the MSGBOX or the txt that AutoIT creates 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