# Command line

## Recommended Posts

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 $step3Cmd = "d:\ClientAdmin\RetailPatch\retailPatch.bat storeregister r07v06 stop-pos-and-change-N"
Run(@Comspec & " /c " & $step3Cmd) Or ShellExecute(@Comspec,$step3Cmd)

• 1

##### Share on other sites

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

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

##### Share on other sites

this is exactly what i wanted, great udf

thanks

##### Share on other sites

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

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

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

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 nacerbaaziz
Hi dear
With this script you can print Unicode text in the CMD screen
the script is  easy to use
just you  write the text that contains Unicode in the first input
and the script automatically reflect the code in the second input
you can copy the text to the clipboard
or you can try printing the text in the CMD window
I apologize to everyone for colors and shape if not appropriate
I'm a blind man and I do not see
Greetings to all of youCmdUtM.au3
• By Simpel
Hi.
Thanks to this post (https://www.autoitscript.com/forum/topic/189553-writing-to-cmd/?do=findComment&comment=1361142) I can now write a helpfile to the cmd when for instance passing parameter -h or -help at cmd. But then cmd is blocked by the script (I had to free it with CTRL+BREAK):
#include <WinAPI.au3> _WinAPI_AttachConsole() $hConsole = _WinAPI_GetStdHandle(1) _WinAPI_WriteConsole($hConsole, "Print helpfile................" & @CRLF) Inside MSDN Library then I found this sentence: A process can use the FreeConsole function to detach itself from its console.
But I don't find something like _WinAPI_FreeConsole(). How can I do it in another way?
• By TheAutomator
A fullscreen console with custom commands!

Introduction:
Hi everyone!
This funny project started as a question in the help section:
https://www.autoitscript.com/forum/topic/174404-edit-detect-key-before-updating-content/

Why would I want it?
You like the old style fullscreen console (like in the old day's), You can add custom commands, You can customize the font a lot more compared to cmd.exe, You can share ideas or add tweaks to the script.
Still to do:
Write a simple custom programming language to implement this tool. Writing a little help file / pdf to describe my little programming language. Add little sound effects like a beep if there is a syntax error (optional). Clean up and modify Console.Au3 content. Add an option to have to type a login password (optional). Make an optional installer that also gives scripts for this tool a custom icon and open with command. ...Call Neo?
Thanks to:
xxaviarxx: debugging, some ideas. jguinch: debugging, adding a bunch of tweaks and ideas. kylomas: debugging, new ideas.
Added usage of tab key in edit control Edit has focus now on startup I'm currently rewriting a simple custom programming language to implement this tool.

Regards
TheAutomator
• 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