Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run vbs as administrator from vbs?

Tags:

Can anyone help me with running vbs from itself but with administrator rights? I need rename computer with Windows 8 via VBScript, but it's possible only if I run my script through administrator command line (CMD → Run as Administrator → runScript.vbs). If I start script with classic CMD the computer isn't renamed.

My idea is I start script with user rights, without parameters and if there is no parameter, the script re-runs itself with admin rights and with parameter as identificator "I'm admin".

Does anyone know how I can do this?

Edit:

I tried this:

If WScript.Arguments.Count = 0 Then     Set objShell = CreateObject("Shell.Application")     objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1 End If 
like image 560
Daphnis Avatar asked Jul 04 '13 09:07

Daphnis


People also ask

How do I run a VBS script as administrator?

To run a script 'As Admin' (with elevated permissions) using VBscript can be done by running ShellExecute and setting the runas flag. This can be used to run an executable, or to run an entire script (batch file or VBScript) with elevated permissions.

How do I run a VBS file?

Click the Start button, and then click Run. In the Open field, type the full path of the script, and then click OK. You can also type WScript followed by the full name and path of the script you want to run.

How do I run a program as administrator without prompt?

Hold down both the Ctrl and the Shift keys on your keyboard and then click or tap on that program's shortcut. You can also use the "Ctrl + Shift + Click/Tap" shortcut on an app's Start Menu tile to run it with administrator permissions in Windows 10.


1 Answers

If UAC is enabled on the computer, something like this should work:

If Not WScript.Arguments.Named.Exists("elevate") Then   CreateObject("Shell.Application").ShellExecute WScript.FullName _     , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1   WScript.Quit End If  'actual code 
like image 181
Ansgar Wiechers Avatar answered Sep 21 '22 12:09

Ansgar Wiechers