Sign in to follow this  
Followers 0
ASut

How to make the cmd run as admin and accept command?

12 posts in this topic

Hi, I need to perform some adminstrative tasks in win 7 by using cmd.exe,

but some of them need admin right (e.g netsh interface ipv4 set subinterface).

The following code don't work,since it do not have admin right

#include <Process.au3>
_RunDos("netsh interface ipv4 set subinterface")

so how can I run the cmd with admin right.

post-39920-0-47190400-1314196809_thumb.p

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Hi, I need to perform some adminstrative tasks in win 7 by using cmd.exe,

but some of them need admin right (e.g netsh interface ipv4 set subinterface).

The following code don't work,since it do not have admin right

#include <Process.au3>
_RunDos("netsh interface ipv4 set subinterface")

so how can I run the cmd with admin right.

post-39920-0-47190400-1314196809_thumb.p

Try this:

$sUser = "username"
$sPass = "pass"
$sDomain = "domain"
$sCMD = "netsh interface ipv4 set subinterface"
RunAs ($sUser, $sDomain, $sPass, 0, "cmd /c " & $sCMD)
Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

#RequireAdmin
#include <Process.au3>
_RunDos("netsh interface ipv4 set subinterface")

Thanks for the help, #RequireAdmin seems it require Admin right to run the au3, but not the cmd command.

Share this post


Link to post
Share on other sites

Try this:

$sUser = "username"
$sPass = "pass"
$sDomain = "domain"
$sCMD = "netsh interface ipv4 set subinterface"
RunAs ($sUser, $sDomain, $sPass, 0, "cmd /c " & $sCMD)

Since the computers are not belong to the domain, and the admin accounts of the pc are not the same, so how can I modify the code to make it more convenience to use, suppose all the currently logged on users have admin right and do not have password?

The currently logged on users name can be obtain by $sUser = @UserName.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Since the computers are not belong to the domain, and the admin accounts of the pc are not the same, so how can I modify the code to make it more convenience to use, suppose all the currently logged on users have admin right and do not have password?

The currently logged on users name can be obtain by $sUser = @UserName.

Are you able to just right click on the script and choose "Run as Administrator"? I guess I'm not fully understanding what it is that you want.

Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

Are you able to just right click on the script and choose "Run as Administrator"? I guess I'm not fully understanding what it is that you want.

Sure, I was able to right click on the script and choose "Run as Administrator", I just want to Disable Autotuning in win 7 by use

"netsh interface tcp set global autotuning=disabled", this command needs to run command prompt as administrator.

Sorry for me poor English.

Share this post


Link to post
Share on other sites

Sure, I was able to right click on the script and choose "Run as Administrator", I just want to Disable Autotuning in win 7 by use

"netsh interface tcp set global autotuning=disabled", this command needs to run command prompt as administrator.

Sorry for me poor English.

So why not just build a simple CMD file and use right click run as admin when you need to run it? This would be the same as opening up a cmd prompt as administrator and typing it in as far as I know.


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

Because it is one of the functions of the program, other functions include change computer name, set computer IP address etc, so I want to make all function in one file.

Moreover, I want to write more code as practice.

Btw, thanks for the help.

Share this post


Link to post
Share on other sites

With windows 7 you might just have to use IsAdmin () and check at the start of your script to make sure they have admin rights before you run it like so:

If Not IsAdmin () Then
    MsgBox (16, "Error", "Admin rights are required to run this program.")
    Exit
EndIf

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites

I use autoit a lot do do something close you want, not in a domain enviroment, but to allow regular users to run certain things under admin credentials without know admin password.

Some commands works in win7, others don't, it's a trial and error.

Local $sUserName = "admin"
Local $sPassword = "passwd"
RunAsWait($sUserName, @ComputerName, $sPassword, 0, @ComSpec & " /c " & 'netsh  interface ipv4 set subinterface', "", @SW_HIDE)

Share this post


Link to post
Share on other sites

Thanks all for the help.

Share this post


Link to post
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
Sign in to follow this  
Followers 0