# How to run a CMD command as administrator?

## Recommended Posts

Hello people, I have a script which calls CMD and executes the netstat command, but when I run it through SCITE the GUI opens but I have no output, How can I run the command as administrator?

Here is my script so far

#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <ScrollBarsConstants.au3>
#include <WindowsConstants.au3>
Local $aWndPos Local$hWnd = GUICreate("Form2", 900, 420, -1, -1, Default + $WS_MAXIMIZE) GUISetBkColor(0xE4E4E4) Local$idFilemenu = GUICtrlCreateMenu("&File")
Local $idExititem = GUICtrlCreateMenuItem("Exit",$idFilemenu)
Local $idInput = GUICtrlCreateEdit("", 210, 10, 660, 360) GUICtrlSetResizing(-1,$GUI_DOCKBORDERS)
GUICtrlSetFont(-1, 10, 400, Default, "COURIER NEW")
Local $idButton = GUICtrlCreateButton("NetStat", 10, 10, 190, 25) GUICtrlSetResizing(-1, BitOR($GUI_DOCKLEFT, $GUI_DOCKTOP,$GUI_DOCKWIDTH, $GUI_DOCKHEIGHT)) GUISetState(@SW_SHOW) While 1$nMsg = GUIGetMsg()
Switch $nMsg Case$GUI_EVENT_CLOSE
Exit
Case $idExititem Exit Case$idButton
GUICtrlSetData($idInput, _CmdInfo() & @CRLF) _GUICtrlEdit_Scroll($idInput, $SB_SCROLLCARET) _GUICtrlEdit_Scroll($idInput, $SB_SCROLLCARET) EndSwitch WEnd Func _CmdInfo($_sCmdInfo = "netstat /b")
Local $iPID = Run(@ComSpec & " /c " &$_sCmdInfo, "", @SW_HIDE,  BitOR($STDERR_CHILD,$STDOUT_CHILD))
; Wait until the process has closed using the PID returned by Run.
ProcessWaitClose($iPID) ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead. Local$sOutput = StringReplace(StdoutRead($iPID), @CRLF & @CRLF, @CRLF) Local$sOutErr = StringReplace(StderrRead($iPID), @CRLF & @CRLF, @CRLF) Local$sReult = $sOutput <> "" ?$sOutput : $sOutErr Return$sReult
EndFunc

##### Share on other sites

Replace the run line with this one :

Local $iPID = Run($_sCmdInfo, "", @SW_HIDE,  BitOR($STDERR_CHILD,$STDOUT_CHILD))

As with cmd it is not elevated, but now netstat is.

Edited by Nine

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites
• Developers

I think you like to see the output as it comes in ...right?

Your script currently waits till it is finished.

Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites
8 minutes ago, Nine said:

Replace the run line with this one :

Local $iPID = Run($_sCmdInfo, "", @SW_HIDE,  BitOR($STDERR_CHILD,$STDOUT_CHILD))

As with cmd it is not elevated, but now netstat is.

Thanks for the help my friend, I changed the line but still I have no output, nothing happens.

##### Share on other sites
6 minutes ago, Jos said:

I think you like to see the output as it comes in ...right?

Your script currently waits till it is finished.

Jos

Thank you Jos for your input, but what I have to change to see output?

##### Share on other sites
9 minutes ago, zoel said:

Thanks for the help my friend, I changed the line but still I have no output, nothing happens.

It is working for me.  You need to click the button, right ?

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites
2 minutes ago, Nine said:

It is working for me.  You need to click the button, right ?

Of course, I click the button but nothing

##### Share on other sites

I see. On win7 the response is immediate.  But on Win10 it takes forever.  This is what @Jos was referring to.

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites
2 minutes ago, Nine said:

I see. On win7 the response is immediate.  But on Win10 it takes forever.  This is what @Jos was referring to.

I see!

##### Share on other sites
• Developers

Use "netstat /b /n" for immediate response as the ip->name is making it pretty slow.

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites
6 minutes ago, Jos said:

Use "netstat /b /n" for immediate response as the ip->name is making it pretty slow.

Yep! That made it work, thank you Sir!!

##### Share on other sites
• Developers

So tell me: Why did you expect it to work fast at all?
Didn't you test it by running the command from the commandline itself on that slow computer?
Debugging isn't really that hard when you put your assumptions in the fridge and test when things aren't doing what you expect.

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites
3 minutes ago, Jos said:

So tell me: Why did you expect it to work fast at all?
Didn't you test it by running the command from the commandline itself on that slow computer?
Debugging isn't really that hard when you put your assumptions in the fridge and test when things aren't doing what you expect.

I did test it by running the command from the commandline, and it runs immediately, I'm on a windows 8.1 PC.

##### Share on other sites
• Developers
3 minutes ago, zoel said:

I did test it by running the command from the commandline, and it runs immediately, I'm on a windows 8.1 PC.

So from the commandline it works fast and slow when ran with Autoit3?

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites
15 minutes ago, Jos said:

So from the commandline it works fast and slow when ran with Autoit3?

Correct!

##### Share on other sites
• Developers

Also when that cmd is ran as Admin as I don't understand why there would be a difference?
For me they are exactly the same.

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

## Create an account

Register a new account

• ### Similar Content

• Hi, I am using python to call the Autoit function. I found a interesting problem.
env:  win10 64bit
python3.6.4  x86、    python3.8.6  x64
autoit v3.3.16.0
code:
#filename: demo.py from ctypes import windll dll = windll.LoadLibrary(r"D:\it_tools\autoit\AutoIt3\AutoItX\AutoItX3_x64.dll") # or AutoItX3.dll dll.AU3_Send("#r", 0) Behaviour:
(1) run with the "python.exe"
it will not open the run dialog, but input a "r" in the cmd window.

(2) run with xxxxxx.exe (renamed from python.exe, you can rename whatever you like)
work success

I dont know why it happened.   I think it shoule be related to Python and Autoit.  So I came here...
Can someone give me some advices.  Thanks a lot.
(by the way, My English is not very well... I wish I  have provided the enough information... If you need more details, please contact me    )

• Hi There !
I have a script here :

;Launch CMD
Run("C:\Windows\System32\cmd.exe")
sleep(2000)
$cmdHandle = WinActivate("C:\Windows\System32\cmd.exe") Sleep(2000) ;Sending document ControlSend($cmdHandle, "", "", "ftp" & @CRLF)
ControlSend($cmdHandle, "", "", "open" & @CRLF) Sleep(2000) ControlSend($cmdHandle, "", "", "first command" & @CRLF)
Sleep(2000)
ControlSend($cmdHandle, "", "", "second-coomand" & @CRLF) first-command and second-command I can't provide cause it's internal. I have complied this .au3 file into an exe and it does the work. But I need to invoke this with Java. Java code I have tried is : ProcessBuilder pb = new ProcessBuilder("C:\\Users\\username\\eclipse-workspace\\Examples\\src\\com\\own\\examples\\etc.exe"); pb.start(); Thread.sleep(5000); Through java it just launches the cmd and nothing happens after that. Please help !! • By Gianni I'm trying to drive a command prompt by sending instructions via a NamedPipe. This way you can (should) be able to send commands to the command prompt and at the same time "view" the result in the same window. This is not allowed if you run a command prompt with "opt_flag" parameters (redirected streams) because this will disable StdOut on the cmd itself. This small (trivial) snippet works for the first command sent to the cmd, but further submissions will fail. Maybe the problem is in how I use the run () command to start a new cmd with the StdIn redirect from the NamedPipe. It seems that the generated cmd will close automatically after the first reception of the command via NamedPipe. suggestions on how to make it work are welcome thanks #include <NamedPipes.au3> #include <WinAPI.au3> ; Creates an instance of a named pipe Global$sPipeName = "\\.\pipe\pipename" Global $hPipe = _NamedPipes_CreateNamedPipe($sPipeName, 1, 1) MsgBox(0, "Debug", "Pipe created. Now open a CMD") ; run a cmd with only StdIn redirected (StdIn data incoming from a pipe) Global $hCMD = Run(@ComSpec & " /K cmd < " &$sPipeName & @CRLF, "c:\") ; ok? MsgBox(0, "Debug", "now Send a command to the cmd via a NamedPipe") _StdInPipeWrite("dir" & @CRLF) MsgBox(0, 'Debug', "further commands will fail" & @CRLF & "now send command 'dir c:\windows'") $sMessage = "Dir c:\windows" & @CRLF _StdInPipeWrite($sMessage) MsgBox(0, 'Debug', "send another command (will also fail)" & @CRLF & "now send command 'echo Hello'") _StdInPipeWrite("echo Hello" & @CRLF) MsgBox(0, "Debug", "end of test") ProcessClose($hCMD) Func _StdInPipeWrite($sMessage) ; =============================================================================================================================== ; This function writes a message to the pipe ; =============================================================================================================================== Local $iWritten,$iBuffer, $pBuffer,$tBuffer $iBuffer = StringLen($sMessage) + 1 $tBuffer = DllStructCreate("char Text[" &$iBuffer & "]") $pBuffer = DllStructGetPtr($tBuffer) DllStructSetData($tBuffer, "Text",$sMessage) If Not _WinAPI_WriteFile( _ $hPipe, _ ; ...... Handle to the file to be written$pBuffer, _ ; .... Pointer to the buffer containing the data to be written $iBuffer, _ ; .... Number of bytes to be written to the file$iWritten, _ ; ... The number of bytes written 0 _ ; ............ [optional] A $tagOVERLAPPED structure or a pointer to it ) Then ConsoleWrite("WriteMsg: _WinAPI_WriteFile failed" & @CRLF & _WinAPI_GetLastErrorMessage()) Else ConsoleWrite("WriteMsg: write OK" & @CRLF & _WinAPI_GetLastErrorMessage() & @CRLF) EndIf EndFunc ;==>_StdInPipeWrite • By Burgs Greetings, I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines. I was thinking of something similar to this: Local$iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if$iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if$hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active...$sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW)$sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;\$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer.
Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
Hi all,
I have a csv file as below, I wand to find srno from csv and send corresponding ip and pass to commend cmd prompt.
Please guide me to create script .
srno,name,ip,pass
1,name1,ip1,pass1
2,name2,ip2,pass2

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...