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

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 people like this

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

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

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

• ### Similar Content

• 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/
I'd like to share this script with everyone that is interested.

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.
What's next?
I'm planning to add my own programming language to it!
Its going to be implemented with an ActiveX COM dll.
The syntax is going to be a bit like Lua I guess, any ideas are welcome!

Todo:
Add sounds. Make an icon / logo. Design a better grammar ( Backus-Naur-vorm: https://nl.wikipedia.org/wiki/Backus-Naur-vorm ). Make test "Console-Scripts" for it. Add a file type for script files ( like batch scripts ). Clean up and modify Console.Au3 content. Add an option to have to type a login password (maybe). Call neo
Thanks to:
xxaviarxx: debugging, some ideas. jguinch: debugging, adding a bunch of tweaks and ideas. kylomas: debugging, new ideas.
Added tab key Main edit has focus now on startup Made a pdf that explains the upcoming programming language (need feedback about it!)
Until the dll is ready you can have fun with what I already have, hope you like it!
UDF can be downloaded from the attachments.
Regards

[The programming language part is been postponed]
[It's been a while, made a custom recursive descent parser in AutoIt, language and updates will be uploaded soon!]

TheAutomator

Console.au3
ConsoleScript.pdf
• 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
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
Starting net on PCNAME... on PCNAME...
net exited on PCNAME with error code 0.
-------------------------------------------------------------------------------
Comment        Administrators have complete and unrestricted access to the computer/domain
Members
The command completed successfully.

## The returned output from running the above script is as follows:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
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
#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?