Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run cmd.exe as administrator in a script

I have a script that I need to run as Administrator just as I would right click on cmd.exe and click Run As Administrator.

Currently this is what I have:

Call WSHShell.Run("cmd.exe /K netdom renamecomputer ... end code")
like image 485
Robert Avatar asked Jul 12 '12 19:07

Robert


People also ask

How do I run a command prompt as administrator in PowerShell?

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.


1 Answers

Have you tried using ShellExecute?

RunCmdElevated.vbs

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd.exe", "/k echo test", "", "runas", 1

wscript RunCmdElevated.vbs

gives

test

C:\Windows\system32>

in a new window. The "1" is the view mode

http://ss64.com/vb/shellexecute.html

like image 107
Jon Swanson Avatar answered Sep 30 '22 15:09

Jon Swanson