# Command line

## 34 posts in this topic

#1 ·  Posted (edited)

I did not see any example like this one

I type that line at the dos  cmd prompt and it's run I am wondering , how to do the same with autoit

$step3Cmd = "d:\ClientAdmin\RetailPatch\retailPatch.bat storeregister r07v06 stop-pos-and-change-N" Edited by antonioj84 error #### Share this post ##### Link to post ##### Share on other sites #2 · Posted $step3Cmd = "d:\ClientAdmin\RetailPatch\retailPatch.bat storeregister r07v06 stop-pos-and-change-N"
Run(@Comspec & " /c " & $step3Cmd) Or ShellExecute(@Comspec,$step3Cmd)

1 person likes this

##### Share on other sites

#3 ·  Posted

thanks so much subz, i had this way however i will change it as above

Run("C:\WINDOWS\system32\cmd.exe")

for instance

Yes,  would that work ? or Processwaitclose($run) Edited by antonioj84 ok #### Share this post ##### Link to post ##### Share on other sites #7 · Posted 9 minutes ago, antonioj84 said: $run=Run(@Comspec & " /c " & $step3Cmd) if not processExist($run) then...

Yes,  would that work ?or Processwaitclose($run) No, the one you posted will not work. It will simply check to see if the process exists or not immediately after running it (which will mean that whatever you put in the code block will get skipped because the process will exist). I would go with $run=Run(@Comspec & " /c " & $step3Cmd) Processwaitclose ($run )

This way, the script will not execute any more code until the process that you initially ran is finished.

##### Share on other sites

#8 ·  Posted

Unfortunately this wouldn't tell you if it's successful or not, its better to get the exit code.  The following UDF by @TheDcoder is great for this, I've used it for cmd, exe, msi installations, it works really well.  All credit to TheDcoder.

2 people like this

##### Share on other sites

#9 ·  Posted

this is exactly what i wanted, great udf

thanks

##### Share on other sites

#10 ·  Posted

@ whiggs , the reason it's a batch file although it finishes there is still  a DOS windows, which I think may indicate falsely that the process still running

however with the command line or the process UDF  you can have an exit code

##### Share on other sites

#11 ·  Posted

19 hours ago, antonioj84 said:

@ whiggs , the reason it's a batch file although it finishes there is still  a DOS windows, which I think may indicate falsely that the process still running

however with the command line or the process UDF  you can have an exit code

If anything, you would probably run into an error like that as the batch script is executing (eg if you are performing installs in the batch script and one of the exe's spawn a separate process for the install and the script moves on to the next install, run into that one before), but that would be confined to the batch script itself, and the batch script would still show as "running" until the commands that it is executing were complete.  But its all good dude.  You don't have to justify why you write your code the way you do to me.  We all have our own way of thinking.  Whatever works, right?

##### Share on other sites

#12 ·  Posted

@whiggs i appreciate your input, however using the processwaitclose will wait forever or  how would  the script knows this  has been completed      ?

the script below is completed.

##### Share on other sites

#13 ·  Posted (edited)

16 minutes ago, antonioj84 said:

@whiggs i appreciate your input, however using the processwaitclose will wait forever or  how would  the script knows this  has been completed      ?

the script below is completed.

6

Well, since the batch file does not include the line "echo off", I can see that the problem could be resolved by removing the last line if you wanted to (PAUSE).  Perhaps replace PAUSE with "timeout 5", which will countdown from 5 and then continue the script.  Or maybe PAUSE was put in there so that you could take a snapshot, in which case the script will know the batch file has finished by assigning a variable to the process handle when the batch file is run, then making the next function "PRocessWaitclose ( $processhandle). If you use the RunWait functions provided in earlier posts, the console window will close after the batch file has finished executing (per the " /c" following @Comspec). This is just a suggestion, but why are you running these commands in an external batch file? Why not run them in autoit using "Run", "RunWait", "ShellExecute", or "ShellexecuteWait"? You could then compile the script as a command line utility and it would display the output for these functions just like what is displayed in the picture you posted and you will have a process handle for each individual command rather than a single one for the batch script, which would offer you more control rather than depending on the the script to successfully execute all of the commands. Edited by whiggs #### Share this post ##### Link to post ##### Share on other sites #14 · Posted (edited) I think you are incorrect try this, these batches are legacy and have pause at the end, and are bake into to the images, therefore using the example below as you suggested will not work, the shellexcutewait command will wait forever ShellExecuteWait("dhcpbat.bat","",@ScriptDir,"",@SW_HIDE) or create any bat files with pause at the end Edited by antonioj84 error #### Share this post ##### Link to post ##### Share on other sites #15 · Posted (edited) 31 minutes ago, antonioj84 said: I think you are incorrect try this, these batches are legacy and have pause at the end, and are bake into to the images, therefore using the example below as you suggested will not work, the shellexcutewait command will wait forever ShellExecuteWait("dhcpbat.bat","",@ScriptDir,"",@SW_HIDE) or create any bat files with pause at the end Ok, so let me make sure I understand this correctly. Are you saying that batch files automatically incorporate a "PAUSE" at the end of the file? I am looking at the text file you sent me, and I can see the Pause included at the end. That is what is causing the script to wait forever. If you get rid of the "PAUSE", it will run to completion. Just to make sure, I did a little test: I created a batch script with the following contents: echo off timeout 4 Then I created an autoit script with the following: RunWait ( @DesktopDir & "\b.bat" ) MsgBox ( 1, "", "It is done" ) Where b.bat is the batch file that I created in the previous step. When executed, the script will run the batch file and, when the script detects that it is finished executing, will display a message box. Worked exactly as I thought it would/described: after the batch file waited for 4 seconds, the batch file process ended and the autoit script displayed the message box. I'm not sure what you are talking about in terms of the PAUSE being built into the script. They are not, you just need to remove the "Pause" line in the txt file you sent. If you need the script to pause for a period of time to see results, I would recommend replacing PAUSE with "timeout n", where n is an integer representing the number of seconds you want the batch script to pause its execution. Edited by whiggs #### Share this post ##### Link to post ##### Share on other sites #16 · Posted says whiggs " Are you saying that batch files automatically incorporate a "PAUSE" at the end of the file? " yes you are correct they are legacy batch files bake into the images and the batch files reside in 5000 computer, removing the "pause" is not an option, the idea is to work around. #### Share this post ##### Link to post ##### Share on other sites #17 · Posted (edited) Ah, I see. I am guessing that this is some kind of deployment that you are doing. In that case, if the batch files are already on the computers, why not just have the script re-write the batch file/create a new batch file without the pause: #include <Array.au3> #include <File.au3> ;FileCopy ( "C:\path\to\batch\file\on\deployed\computers\oldscript.bat", "C:\path\to\batch\file\on\deployed\computers\newscript.bat" )$filelines = FileReadToArray ( "C:\path\to\batch\file\on\deployed\computers\oldscript.bat" )
$remove = 0 For$i = 0 To UBound ( $filelines ) - 1 Step 1 If StringLeft ($filelines[$i], 5 ) = "netsh" Then$filelines[$i] =$filelines[$i] & ' > "C:\output\for\the\netsh\command\output1.txt"';modifies the batch file lines containing the commands which contain information to output that information to a txt file on the local machine. EndIf If$filelines[$i] = "PAUSE" Then$remove = $i EndIf Next _ArrayDelete ($filelines, $remove )$file = FileOpen ( "C:\path\to\batch\file\on\deployed\computers\newscript.bat", $FO_OVERWRITE ) _FileWriteFromArray ($file, $filelines ) FileClose ($file )
RunWait ( "C:\path\to\batch\file\on\deployed\computers\newscript.bat" )

#cs
Code to gather the information which the new batch file outputs to the txt files can be put here.
See, there are a million and one ways to skin a cat.
#ce

Again, these are just solutions.  If you already have your solution and it works for you, all power to you.  This is just a potential solution.

Edited by whiggs

##### Share on other sites

#18 ·  Posted

great you understand the issue, thank you for the snippet, and your suggestion,  this is one  option worth looking into, not sure if they will approve it.

##### Share on other sites

#19 ·  Posted

5 minutes ago, antonioj84 said:

great you understand the issue, thank you for the snippet, and your suggestion,  this is one  option worth looking into, not sure if they will approve it.

Isn't corporate red tape just the worst...  Fingers crossed.  And the portion that comes after the script is run that collects the information from the text files, you can always read the contents of the files into an array (like i did with "oldscript", output the information in your autoit script, and then delete the output files..  Hopefully it works out for you.

##### Share on other sites

#20 ·  Posted

yeah there is no work around beside using that process.udf as suggested by SUBZ, i will use that instead, you will notice below  i got my exit 0 for success 1 for failure.

## Create an account

Register a new account

• ### Similar Content

• By hcI
Hello I would like to know if there is a way to return a sentence in cmd when I launch from it (because I add arguments).
For example, diskpart.exe which help to manage the key and hdd connected, when you launch it with the parameter "/f" the app return a sentence saying that it don't recognize the parameter "/f" and it return the sentence in the cmd where i started the application, not a new one.
That's what I want to do but I couldn't find anything that would solve my problem on internet and on AutoIt like ConsoleWrite / ConsoleWriteError (don't work).

Thanks
• By Duck
I'm attempting to capture the output from the command line tool PSEXEC. I'm using AutoIT to run an instance of PSEXEC against a remote PC to audit Local Admins in my environment using net.exe (C:\Windows\System32> net localgroup administrators). However the usual trick I use to capture command line output does not appear to work well with PSEXEC, as the bottom portion of the output is missing from the return. Any ideas or recommendations are greatly appreciated.

Here is what I'm working with:
;This script will read from a list of hosts and report who has local admin privileges on the machine #RequireAdmin Global $fileName = @ScriptDir & '\test.txt' ;hostlist, one host per line readHostList() ;Read list of hosts Func readHostList() Local$file = FileOpen($fileName, 0) While 1$line = FileReadLine($file) If @error = -1 Then ExitLoop ConsoleWrite($line & @CRLF) ;MsgBox(0,0,$line) getLocalAdmins($line) WEnd FileClose($file) EndFunc ;run PSEXEC to list local admins Func getLocalAdmins($remotePC) Local $testFile = @ScriptDir &'\test234.txt' FileOpen($testFile, 1) Local $psexec = 'psexec \\' &$remotePC & ' net localgroup administrators' FileWriteLine($testFile, _RunCmd($psexec) ) FileClose($testFile) EndFunc ;Used to return CLI output Func _RunCmd($sCommand) Local $nPid = Run(@Comspec & " /c" &$sCommand, @SystemDir, @SW_Hide, 8), $sRet = "" If @Error then Return "ERROR:" & @ERROR ProcessWait($nPid) While 1 $sRet &= StdoutRead($nPID) If @error Or (Not ProcessExists ($nPid)) Then ExitLoop WEnd Return$sRet EndFunc
## If i manually run the command on the remote PC via PSEXEC I will get the following output:
PsExec v2.11 - Execute processes remotely
Sysinternals - www.sysinternals.com
Starting net on PCNAME... on PCNAME...
net exited on PCNAME with error code 0.
-------------------------------------------------------------------------------
Members
The command completed successfully.

## The returned output from running the above script is as follows:
PsExec v2.11 - Execute processes remotely
Sysinternals - www.sysinternals.com
Connecting to PCNAME...
Starting PSEXESVC service on PCNAME...
Connecting with PsExec service on PCName...
Starting net on PCNAME..
net exited on PCNAME with error code 0.

**Note to test this script PSEXEC must be in the system dir or the path in the script changed
• By luckyluke
Hello,
Im trying to read the output from CMD using Dllcall, here is my code:
#include <WinAPI.au3> #include <array.au3> Global Const $STD_OUTPUT_HANDLE = -11 Global Const$_CONSOLE_SCREEN_BUFFER_INFO = _ "struct;int dwSizeX;" & _ "short dwSizeY;" & _ "short dwCursorPositionX;" & _ "short dwCursorPositionY;" & _ "short wAttributes;" & _ "short Left;" & _ "short Top;" & _ "short Right;" & _ "short Bottom;" & _ "short dwMaximumWindowSizeX;" & _ "short dwMaximumWindowSizeY;endstruct" $pCmd = Run( "cmd.exe" ) Sleep(1000)$hCmd = WinGetHandle("") ConsoleWrite('handle:' & $hCmd & @CRLF)$aRet = DllCall("kernel32.dll", "int", "AttachConsole", "dword", $pCmd) ;_ArrayDisplay($aRet) If $aRet[0] <> 0 Then$vHandle_data='' $vHandle=''$vHandle_data = DllStructCreate($_CONSOLE_SCREEN_BUFFER_INFO) ; Screen Buffer structure$aRet1 = DllCall("kernel32.dll", "hwnd", "GetStdHandle", "dword", $STD_OUTPUT_HANDLE) if not @error Then$vHandle = $aRet1[0]$aRet = DllCall("kernel32.dll", "int", "GetConsoleScreenBufferInfo", "hwnd", $vHandle, _ "ptr",$vHandle_data) MsgBox(0, '1',DllStructGetData(\$vHandle_data, 'dwSizeX') & _WinAPI_GetLastErrorMessage()) EndIf It did not work, i got the message 'The handle is invalid'. Please help?
• By mihaijulien
Hello,
I compiled a script I made that takes a command line parameter (the version of a .msi installer) when launched. The script was compiled with the /console option. The script (.au3) works fine but the executable returns  the following error:
Error: array variable has incorrect number of subscripts or subscript dimension range exceeded
• By fosil
Hi everyone.

I'm currently working a program that constantly prints out log files through "consolewrite" and the "#AutoIt3Wrapper_Change2CUI=y" wrapper.

Part of this program requires me to run a batch script.

My issue is the batch script launches from the same window as consolewrite. I need the batch file to be launched through a different window as currently this causes an issue with the logs (which need to be very precise) but also causes the batch file to produce some funny behavior...

Does anyone know how I can force the file to run on a second DOS window?