[VBScript] I want to automatically promote VBS to administrator execution!How to promote an administrator in a script executed by double-clicking.

Windows

Hello!This time I would like to create a script that can be executed with administrator privileges by double-clicking VBScript.

I've introduced scripts that automatically acquire administrator privileges at runtime in batch and Powershell as well, but I could implement it in VBS in a similar way.

Unlike batch, VBS can perform complex processes and, unlike Powershell, it doesn't require any preparation to run.In addition, it is easy to encrypt, so it has a wide range of activities.I think it will come in handy in sites where there are many users who are not familiar with it.

Since it is such a VBS, if you can get administrator privileges just by double-clicking, you can perform more detailed processing.

Let's do it right away!

Thing you want to do

・ I want to process VBS with administrator privileges by double-clicking.

This time, even those who do not understand anything in particular will be able to execute it with only the most rudimentary "double click".

Script example

'①ダブルクリックで実行されたか判別
If WScript.Arguments.Count = 0 Then

'②管理者に昇格して実行
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ dummy", "", "runas", 1

'ex.管理者権限じゃなかったら終了
WScript.Quit
End If

Commentary

① Determine if it was executed by double-clicking

First, describe the process when it is executed by double-clicking.
In any software, when you double-click it, it means "there are no arguments given to the program".

When counting the number of arguments, if it is "XNUMX", the program can be determined to have been double-clicked.

If WScript.Arguments.Count = 0 Then

"WScript.Arguments.Count" here returns the number of arguments given to VBS.
In other words, if this is "XNUMX", it can be determined that it was executed by double-clicking.

You can branch the IF here and execute the contents of the IF if it is executed by double-clicking.

② Promote to administrator and execute

Next, it will be the part to be executed by the administrator.
This time, I am writing a program that uses a shell object to "execute the script itself with administrator privileges".

Set objShell = CreateObject ("Shell.Application")
objShell.ShellExecute "wscript.exe", "" "" & WScript.ScriptFullName & "" "dummy", "", "runas", 1

"ObjShell.ShellExecute" is the command to "execute the program"."Wscript.exe" which is a VBScript execution program is specified for this.If you add the path of the script to this argument, it will be executed.So I'm passing "WScript.ScriptFullName" to mean myself.

WScript.ScriptFullName will return the full path of the script you are currently running.In this case, double-click to execute the script itself.This will ensure that it will re-execute itself no matter where you place the script.

* It is necessary to add "" "on both sides so that it is okay if there is a space in the script path. Since "" "itself is treated as an instruction in VBS, many are attached to make" "" recognized correctly.

The next important thing is to specify "runas".
If this is specified, this ShellExecute will execute the program with administrator privileges.

Finally, you have to give an argument to determine what you did yourself.
"Dummy" is attached to the full path as an argument.When it is re-executed with this, the given argument explained in (XNUMX) becomes one, so it is determined that the administrator could execute it as it is, and the process proceeds to the next step.

This is a mechanism that allows you to execute later processing with administrator privileges!

Afterword

This time, I introduced a script that automatically promotes to administrator privileges with VBScript.

Although VBS has been around for a long time, it is still a script that can still be used in active use.
I was wondering if it would replace Powershell, but for some reason I still can't easily double-click it, PS.

Since batches are also variables and syntax are special, they are often a little out of reach, and if you want to do complicated processing, VBS will be better off. (Well, the short ones are batches at all, though ^^;

That's why, by all means, please implement automatic administrator promotion in VBS and execute more free and discerning processing ♪

I hope it helps you.

Comment

Translate »
I copied the title and URL