Unlock For Us

How to Run VBScripts as Administrator in Windows Vista

Some programmers are asking why their Restore Point Creator Program written in Vb.Net/C# doesn't work in Windows Vista. It will not work because of UAC. To run the program, you must open or launched it as administrator.

The same way with VB Script programming in Windows Vista. I wrote a simple alternative way to launch scripts as Administrator.

Here:

If WScript.Arguments.Count > 0 Then

'Your VB Script CODE here

Else

Set objLaunch = CreateObject("Shell.Application")
obj
Launch.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdmin", , "runas", 1

End If

It will elevate the script before launching your code.

Read more: Universal Script Template in Elevating Commands as Administrator in Windows Vista

Easy? Enjoy!

More Articles:

How to Get the Windows Experience Index (WEI) score of a Network Computer

Desktop Window Manager Command list

YouTube Hack!

1 Comment:

Anonymous said...

i'd like to learn how to run automated background (not interactive) scripts which require elevation. just my simple daily/weekly/etc maintenance scripts require this (doesn't everybody have such scripts?). there are several pages on your site that seem to address this (i think? hard to tell), this being one such. various different techniques are illustrated, but i can't get any of them to work. i can get them to work in the sense that they execute the desired code, and they execute it with my admin token, but they still PROMPT me INTERACTIVELY to type alt-C (CONTINUE). the whole point (for me) is to avoid that. is this possible? if so, do any of your approaches on these web site pages show how?

btw: a comment on teaching technique. if you want to teach a concept, then the best approach is to teach just that key idea. after that, you can add frills. e.g., the first page i read showed a script that required arguments and had a little loop to loop through all arguments supplied. almost all of the script was about arguments, which are irrelevant to the key idea you're teaching. so, first do the simplest thing you can. maybe run the "type" cmd in elevated mode. strip out the other. later, if you want (on that page or another page about handling arguments) teach how to handle arguments, fine. don't mix up the two. :) similarly, the teaching of script elevation is mixed up with the teaching of adding right-click menu commands. totally unrelated. if you want to teach that, put it on another page and teach script elevation concept by itself without all the other stuff hiding the real lesson.

the otheer page(s) solution is to run (objapp.shellexecute) where it executes cmd with arguments that specify the actual (cmd-based, i guess, not gui) apps. it has a reference to wscript in it, but uses instead cmd. is that legal? this page apparently runs a wscript rather than cmd script, but no example of how to invoke a wscript, or what the purpose or advantage of that is compared to cmd. why did you switch? do either of them do what i want (elevate in a background script)?

also it would help if you gave a little explanation about what's going on. e.g., the script on this page executes "your script" only if there are arguments. why? and if there aren't it executes something else, which requires a script name. what is that second script and what does it have to do with executing my code (in green) that doesn't appear in the else branch at all! do you mean to invoke the same script recursively? or what?

thanks for any help. :)

vista should be called the security release. and should come with examples of maintenance scripts and how to run elevated and how to work around all the other security restrictions that continually get in the way. i like security. i just need to know how to do my work.

also, fyi, i have discovered one way: use the task manager and click the checkbox to run with the highest privileges (on general tab of task creation). that works. but there are times where the task manager is not appropriate and i'd like to do the same thing. i'm sole owner of this machine. shouldn't i be able to? i am an administrator, but that's not enough. i also activated the hidden Administrator (with capital A) account that does not use uac (runs like xp), but that's not appropriate always either; e.g., it won't operate in my user context (documents, etc.).

someone must know. there must be people writing scripts that require elevation. searching the web and microsoft haven't helped yet.

another question: you refer to windows objects and object model; e.g., objapp.shellexecute method. great. is there documentation anywhere on this object model? what types/classes/methods are available and what they do? i'm a home user and not prepared to pay thousands of $ for a(multiple) development environment(s). i just want a couple maintenance scripts, which any end user would want. and i paid for windows. but no documentation comes. :(

 

© Unlock For Us @2024| Blogger| License Agreement