run cmd.exe as administrator in a script
Asked Answered
B

1

7

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")
Benzyl answered 12/7, 2012 at 19:41 Comment(0)
E
9

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

Electrify answered 22/7, 2012 at 16:1 Comment(1)
When I do this I get a User Account Control dialog. How do I grab that dialog and click the 'Yes' using SendKeys?Serotonin

© 2022 - 2024 — McMap. All rights reserved.