How to run WSL programs within AutoIt? - windows-subsystem-for-linux

I'm trying to run a linux console application using AutoIt.
So far I was successful in generating a batch file with the following command:
wsl /home/ggeorgiev/DD/myprogram --json_file /mnt/c/Users/ggeorgiev/my_input.json
This batch runs successfully from cmd, powershell and also when I just double click on it in the explorer. So, I'm thinking that part is okay.
In AutoIt, I'm trying to run the same batch file ("JBDD_start.bat") using the following function:
Func RunJBDD()
Local $iPID = Run("cmd.exe " & "JBDD_start.bat", #WorkingDir, #SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD,$RUN_CREATE_NEW_CONSOLE ))
Local $sOutput = ""
While 1
$sOutput &= StdoutRead($iPID)
If #error Then ; Exit the loop if the process closes or StdoutRead returns an error.
ExitLoop
EndIf
MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
WEnd
$sOutput = ''
While 1
$sOutput &= StderrRead($iPID)
If #error Then ; Exit the loop if the process closes or StderrRead returns an error.
ExitLoop
EndIf
MsgBox($MB_SYSTEMMODAL, "Stderr Read:", $sOutput)
WEnd
EndFunc
It doesn't show anything in the StdErr nor in StdOut message boxes.
Any suggestions on what may have gone wrong or how to find what happens "behind the scenes" are highly appreciated.

Try this, maybe it helps to get your output.
ConsoleWrite( _getDOSOutput('ping 4.2.2.2') & #CRLF)
Func _getDOSOutput($command)
Local $text = '', $Pid = Run('"' & #ComSpec & '" /c ' & $command, '', #SW_HIDE, 2 + 4)
While 1
$text &= StdoutRead($Pid, False, False)
If #error Then ExitLoop
Sleep(10)
WEnd
Return $text
EndFunc ;==>_getDOSOutput

I'm not familiar with AutoIt, but I'm going to take an educated guess here. If I'm wrong, at least it might help someone else.
I notice that AutoIt comes with both 32-bit and 64-bit executables. Make sure you are either:
Using the 64-bit version to execute your batch file
Or, if you really do need to run the 32-bit version of AutoIt, call C:\Windows\Sysnative\wsl.exe

Related

manual entry in cmd window works, VBA executing CMD works, but not VBA when I use run (so I can hide the window)

SECOND EDIT/UPDATE: tried the path change recommendations, did not see any changes to the command string, still does not work. I re-wrote the code to use a fixed text file instead of a random temp file so I could monitor the contents of the file during execution. Able to conclusively show it is the
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
code line that doesn't behave as expected. Still works with the w32tm command line, but not with the ntpq command line. With ntpq command, no changes made to the file, no error flags. I also tried out (again) the exec version of this problem where the window is supposed to flash a bit before it gets hidden programmatically. I get the expected reslut using exactly the same command string, cut and pasted into the other code. So the same command line works with manual entry into CMD, into PowerShell, and in the .exec code version, not the .run code version.
End of second edit. -------------------
EDIT: more debugging... ntpq -p works if I do .exec instead of .run, but then of course can't hid the cmd window. Extra test code at the end.
This Works: If I run these two commands in manually opened cmd window, or PowerShell window, both give the expected results.
w32tm /stripchart /computer:time.nist.gov /dataonly /samples:3 /rdtsc /period:1
ntpq -p
The second, ntpq -p, is bundled with NTP windows software from the home of the Network Time Protocol project that gives similar information to windows' w32tm when NTP is set up to look at the same time service computer as in the w32tm command.
This Doesn't work:
When I try to use these two command string when running CMD functions hidden using the classic "write to file" method shown in SO here and other places, the w32tm version gives the same results as the manual version, but the ntpq version just returns "error".
I read every single one of the recommended links for this question as well as searching OS and Google, and have not found an answer.
I am stuck on next step to troubleshoot the problem...only thing I could think of was to run the commands manually to confirm they work there. I can't imagine it being a administrator privileges issue since I can run them both in CMD line or PowerShell windows opened at normal rights level.
What should I look at next?
Here is the test code.
Option Explicit
Sub TestShellRun()
Dim sCmd As String, sReturnNTP As String
sCmd = "w32tm /stripchart /computer:time.nist.gov /dataonly /samples:3 /rdtsc /period:1 " ' /packetinfo"
sCmd = "%ComSpec% /C %SystemRoot%\system32\" & sCmd
sReturnNTP = fShellRun(sCmd) 'good return value, same as manual cmd line
Debug.Print sReturnNTP
sCmd = "ntpq -p"
sCmd = "%ComSpec% /C %SystemRoot%\system32\" & sCmd
sReturnNTP = fShellRun(sCmd) 'ERROR return value, even though manual cmd line has good values
Debug.Print sReturnNTP
End Sub
Public Function fShellRun(sCommandStringToExecute) As String
' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.
' "myIP" is a user-selected global variable
Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead
Dim iErr As Long
Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
On Error Resume Next
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
iErr = Err.Number
On Error GoTo 0
If iErr <> 0 Then
fShellRun = "error"
Exit Function
End If
On Error GoTo err_skip
fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile, 1).ReadAll
oFileSystemObject.DeleteFile sShellRndTmpFile, True
Exit Function
err_skip:
fShellRun = "error"
oFileSystemObject.DeleteFile sShellRndTmpFile, True
End Function
sCommand = "ntpq.exe -p"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(sCommand)
strOutput = WshShellExec.StdOut.ReadAll
Debug.Print strOutput
Your fShellRun function didn't work due to error in temporary file path. Here is fixed version.
Function fShellRun(sCommandStringToExecute) As String
...
'invalid file path without path separator between directory path and filename!
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & _
oFileSystemObject.GetTempName
'valid path with path separator between directory path and filename
sShellRndTmpFile = oFileSystemObject.BuildPath( _
Environ("temp"), oFileSystemObject.GetTempName)
...
End Function

Return the status of an FTP upload using WinSCP batch scripting executed from VBA?

I am sharing an Excel workbook with multiple users who are executing a macro that executes the following WinSCP batch script:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/command ^
"open ftp://user:pass#ftp.website.org/" ^
"cd /incoming/data" ^
"put ""%~dp0file.txt""" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
The script is executed from VBA as follows:
Call Shell("C:\Users\" & Environ("username") & "\Sharepoint - Library Folder\FTP\ftpupload.bat")
When executed, the command window appears for 1-2 seconds and goes away. Is there a way to leave it up with the Success/Error result or even better would be to pass it back to VBA so I can display the result in an Ok-Window?
Note: I'd like to avoid having to register the WinSCP COM in VBA as this workbook is being used by multiple people and I need to keep it simple with as little prerequisites as possible.
Your batch file already returns exit code indicating an error/success.
So all you need is to capture the code and act accordingly.
For that, see Is it possible to return error code to VBA from batch file?
Set oSHELL = VBA.CreateObject("WScript.Shell")
Dim exitCode As Integer
exitCode = oSHELL.Run("""C:\Users\" & Environ("username") & "\Sharepoint - Library Folder\FTP\ftpupload.bat""", 0, True)
If exitCode <> 0 Then
MsgBox "Failed", vbOKOnly, "Failure"
Else
MsgBox "Succeeded", vbOKOnly, "Success"
End If

Output text File creation/saving VBA code timing issue

Problem Summary: Creating a text file for FTP commands is not finishing by the time the command file needs to be called. It won't trap with a "if file exists". How to make sure the file is created before continuing with the code?
Details: I'm working on an portion of code in my Excel workbook to FTP some files. To do that, I'm creating a FTPcmd.txt file via code containing the FTP commands, closing the file and then shelling the FTP command in the CMD window. It looks like the command file is taking too long to complete the write and, therefore, I'm getting an "Permission Refused" error. The FTP log says "Error opening script file C:\temp\FTPcmd.txt." I am error checking to see if the file exists, but I think the file shows up as existing after the open statement, not the close. I'm not hitting the else statement in the DIR <>"" The IsFileOpen function was found on "http://www.vbaexpress.com/kb/getarticle.php?kb_id=468 VBA Express : Excel - Check If a File Is Already Open"
I don't believe it's a problem ShellWait problem. I need the file to write before I call the Shell.
If I step through it manually, it works, after I see the file appear in the directory.
I'm also piping the FTP output to a file and reading it back in for success/failure messaging in a similar manner and I'm getting the same problem with that.
Anyone have any ideas? Thanks in advance!
Open temppath & "FTPcmd.txt" For Output As #2
Print #2, "user " & FSOUserName
Print #2, FSOpw
Print #2, "lcd " & temppath
Print #2, "cd public_html"
Print #2, "binary"
Print #2, "mput " & Chr(34) & "index.htm" & Chr(34)
Print #2, "cd .."
Print #2, "cd public_ftp"
Print #2, "mput " & Chr(34) & myfilename & Chr(34)
Print #2, "bye"
Close #2
Start = Timer
FTPlooper:
If Timer - Start > 30 Then saveme = 1: Text = Text & " FTP Failure": GoTo failpoint
If Dir(temppath & "FTPcmd.txt") <> "" And IsFileOpen(temppath & "FTPcmd.txt") = False Then
Shell "cmd /c ftp -n -i -g -s:" & temppath & "FtpCmd.txt " & FSOHostURL & ">" & temppath & "ftpout.txt 2>&1"
Else
GoTo FTPlooper
End If
Here's the function I'm using to see if the file is open.
Code:
Function IsFileOpen(FileName As String)'http://www.vbaexpress.com/kb/getarticle.php?kb_id=468
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select
End Function
Output of FTPout.txt:
Error opening script file C:\Users\Theresa\Downloads\FTPcmd.txt.
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer]
[-r:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [host]
-v Suppresses display of remote server responses. -n
Suppresses auto-login upon initial connection. -i Turns
off interactive prompting during multiple file
transfers. -d Enables debugging. -g Disables filename globbing (see GLOB command). -s:filename
Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts. -a Use any local interface when binding data connection. -A login as anonymous. -x:send sockbuf Overrides the default SO_SNDBUF size of 8192. -r:recv sockbuf Overrides the
default SO_RCVBUF size of 8192. -b:async count Overrides the
default async count of 3 -w:windowsize Overrides the default
transfer buffer size of 65535. host Specifies the host
name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.

How to stop script from automatically closing?

I open an .exe file but the script immediately closes after opening it. How can I prevent the script from closing?
Local $engine= "C:\Users\Davis\Desktop\chessEngine\stockfish-5-win\Windows\stockfish_14053109_32bit.exe"
Run($engine, "", #SW_MAXIMIZE, $STDOUT_CHILD)
Removing $STOUT_CHILD from Run() leaves the script open after executing, but I need this to read output from the program. Why is this happening?
Local $engine = "C:\Users\Davis\Desktop\chessEngine\stockfish-5-win\Windows\stockfish_14053109_32bit.exe"
Local $iPID = Run($engine, "", #SW_MAXIMIZE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
This ist an example how to get output from a DOS command.
ConsoleWrite(_getDOSOutput('ipconfig /all') & #CRLF)
Func _getDOSOutput($command)
Local $text = '', $Pid = Run('"' & #ComSpec & '" /c ' & $command, '', #SW_HIDE, 2 + 4)
While 1
$text &= StdoutRead($Pid, False, False)
If #error Then ExitLoop
Sleep(10)
WEnd
Return StringStripWS($text, 7)
EndFunc ;==>_getDOSOutput

show a trackbar or image while shell() is running a cmd.exe instruction

I'm trying to do a simple windows form application in visual basic that change the attributes of all the files in a drive using shell().
It works but since sometimes there are a lot of files, the applications looks like it freeze (because I'm using the "wait" argument as true.
So I'm looking for a way to show an "load" gif while the cmd is doing his thing, I found and example in msdn for a "wait until process finish" using interaction.Start (http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.interaction.shell(v=vs.110).aspx) I tried to mix shell with that example but I can't get it to run ok.
This is my code so far.
pic_working.Visible = True
myDrive = "F:\"
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.GetProcessById(Shell("cmd.exe /C attrib -r " + myDrive + "*.* /s /d", AppWinStyle.NormalFocus, False))
procID = newProc.Id
Dim procEC As Integer = -1
If newProc.HasExited Then
procEC = newProc.ExitCode
End If
MsgBox("Process with ID " & CStr(procID) & " terminated with exit code " & CStr(procEC))
myDrive =""
pic_working.Visible = False
It works...kind of... when I set the wait argument for shell() to "true" it take like 30 seconds to complete the task (in my test drive), but with it to false it just skips everything and my pic_Working is never showed.
Can you give me a hint... it's possible to do this in this way, or I have to do the long way (using File.SetAttributes and parsing one file at the time?
Thanks!
Disclaimer: C++ guy here.
Set Wait to False. If Shell tells you the program is still running, display your image, start a Timer, Use the Win32 API OpenProcess with the PID returned by Shell and save the HANDLE, and return. On each Timer Tick, use the Win32 API WaitForSingleObject with the HANDLE and with a timout equal to 0. If the process handle is successfully signaled, then the task is terminated.
EDIT
How to know when a process is terminated, when you have successfully got an HANDLE to that process with OpenProcess:
HANDLE hProcess;
[...]
DWORD dwRet = WaitForSingleObject( hProcess, 0 );
if ( dwRet == WAIT_OBJECT_0 ) {
// Process is terminated. Don't forget to close the handle
CloseHandle( hProcess );
} else if ( dwRet == WAIT_TIMEOUT ) {
// Process NOT terminanted, wait next Timer Tick
} else {
// Error Occurred
DWORD dwLE = GetLastError();
}
Using the Win32 HANDLE type in C#/VB: use IntPtr