Run as Administrator in VBScript/JScript (WSH)


In last post, we show you how to run an external program using Administrator (run-as). The powershell doesn’t come by default on home-use editions but on server editions (however, you can get Powershell installed in Control-Panel Programs and Features).

The WSH (Windows Scripting Host) is widely used because it comes almost on every Windows installation from XP. The *.vbs (VBScript) and *.js (Microsoft JScript) scripts can be interpreted by cscript.exe (console) or wscript.exe (windows).

We can create COM object Shell.Application and elevate the administration permissions using ShellExecute

VBScript

Save the following as runas.vbs and run at command prompt: cscript.exe runas.vbs command parameters

Dim command
command = WScript.Arguments.Item(0)
Dim argument
argument = ""

For i = 0 To WScript.Arguments.Count
	argument = argument & WScript.Arguments.Item(i) & " "
Next

Dim shellapp
Set shellapp = CreateObject("Shell.Application") 
shellapp.ShellExecute command, argument, null, "runas", 1

JScript

JScript is the Microsoft’s implementation of Javascript, it supports error handling try-catch in case something goes wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
var command = WScript.Arguments.Item(0);
var argument = "";
for (var i = 0; i < WScript.Arguments.Count(); ++i){
  argument += WScript.Arguments.Item(i) + " ";
}
 
try{
  var shellapp = new ActiveXObject("Shell.Application");
  shellapp.ShellExecute(command, argument, null, "runas", 1);
}
catch(e){
  WScript.Echo("Exeception: " + e.description);
}
var command = WScript.Arguments.Item(0);
var argument = "";
for (var i = 0; i < WScript.Arguments.Count(); ++i){
  argument += WScript.Arguments.Item(i) + " ";
}
 
try{
  var shellapp = new ActiveXObject("Shell.Application");
  shellapp.ShellExecute(command, argument, null, "runas", 1);
}
catch(e){
  WScript.Echo("Exeception: " + e.description);
}

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
288 words
Last Post: Run as Administrator in Powershell
Next Post: Static Object, Global Variable, Lazy Loading in C#

The Permanent URL is: Run as Administrator in VBScript/JScript (WSH)

Leave a Reply