Jump to content

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


ASut
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

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

Link to comment
Share on other sites

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.

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

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

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

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...