Previous Posts


Windows Vista not optimized for solid-state drives...

Make sure you patch your DNS Servers

Performance Tuning for Windows 2008 Server

Find out when the last reboot occurred (Updated)

Microsoft Hyper-V (I'm Impressed!)

Vista/2008 Remote Control Beta

IntelliAdmin LAN Beta

Free Windows Security Guides

How to purchase Windows XP after June 30th

Run Sysinternals directly from the Internet



Archives

May 2005

January 2006

April 2006

May 2006

June 2006

July 2006

September 2006

October 2006

November 2006

December 2006

January 2007

February 2007

March 2007

April 2007

May 2007

June 2007

July 2007

August 2007

September 2007

October 2007

November 2007

December 2007

January 2008

February 2008

March 2008

April 2008

May 2008

June 2008

July 2008

August 2008

September 2008

October 2008

November 2008



Subscribe to our Feed:






How to elevate your scripts in Vista

If you like this article, then sign up for our email newsletter to get more like it every day in your inbox

Now that Vista is starting to permeate the enterprise...I have started to get quite a few questions like this:

Steve, we just replaced 10 machines, and they all are running Vista. We have a 'one time' script that we need to roll out that requires administrative access. Every time we run it, it fails, and the users *are* administrators of their own machines. Know any way around this?

Yes. The reason why the script is failing is because Vista executes it with limited access - even if they are an administrator.

To get Vista to run a program as an administrator, you need the program to be elevated to full access. Vista will only show the elevation (UAC) prompt if the application requests it, or if it is detected that the program is actually an installer.

If you have not had the pleasure of using Vista yet (I am joking), it looks like this:

Vista UAC Prompt

To force Vista to request administrative access, you need to have two scripts. The first script will force the prompt, and if accepted it will call the second script (The one you wanted to run) with the elevated administrative rights.

With some creativity we can distill it down to one script. This is accomplished by having the script call itself.

I have put something together that does just that... Just add this code to the start of your script, and it will force a UAC prompt.


'This flag is used to determine if we are being called the first time
'or if we are being called the second time with elevated access
Dim bFoundUAC
'The sSA object will allow us to execute applications
Set sSA = CreateObject("Shell.Application")

'Set our flag to the default
bFoundUAC = FALSE

'See if we are inside the second calling of the script
if (WScript.Arguments.Count=1) then
 if (WScript.Arguments(WScript.Arguments.Count-1)="UAC_PROMPT") then
  bFoundUAC = TRUE
 end if
end if

'If we are not called the second time, then call the script with elevated access
if (bFoundUAC=FALSE) then
 'Build our script command line
 sCommandLine = Chr(34) & WScript.ScriptFullName & Chr(34)
 'Build our arguments
 for iCount=0 to WScript.Arguments.Count-1
  sArguments=sArguments & " " & Chr(34) & WScript.Arguments(iCount) & Chr(34)
 Next
 'Add our trigger to the end
 sArguments=sArguments & " UAC_PROMPT"
 sArguments=sCommandLine & sArguments
 sSA.ShellExecute "wscript.exe", sArguments, "", "runas", 1
 'Since we are re-launching the script we exit now
 WScript.Quit
end if

'Your usual script code would be placed here


One thing to note is this script works by adding an argument to the end of all the arguments you originally passed it. This is how it knows that it has been elevated. Otherwise it would keep running itself in an infinite loop. In most cases this will not be a problem, but it is something to check if you do use arguments in your script.

Bonus: This script will also prompt for elevation when your XP users are not administrators:



Actually...I think it will prompt for elevation no matter what.

Finally, this method is much better than the previous article on the subject - since you don't need anything extra to elevate your script.


Posted By: Steve Wiseman on Monday, July 28, 2008

Check out our utilities for windows

 



Copyright © IntelliAdmin, LLC, 2008. All Rights Reserved