Jump to content

How to run a CMD command as administrator?


zoel
 Share

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

 

Link to comment
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
Link to comment
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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?

Link to comment
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 ?

Link to comment
Share on other sites

  • Developers

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

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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. ;) 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

Thanks for the advice.

Link to comment
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...