Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making scripts UAC aware

Scripting for the Enterprise

Making scripts UAC aware

by  markdmac  Posted    (Edited  )
[color #204A87]Detecting if UAC is Enabled or Not Set
MarkDMac's Enterprise Scripts[/color]

While working on a project for a customer that had a mix of Windows XP and Windows 7 computers, I encountered some trouble with getting a script to run on the test machine that had UAC enabled. Initially I just added code to always launch the script elevated, but then reports came in that the code would not execute on the Windows XP systems. I had to add in some code to detect the Windows version. That was all well and fine for the needs of the workstations, but then it dawned on me that Windows Server 2008 can also have UAC enabled, so after a little research I found where UAC is set in the registry and altered my template to no longer detect OS but detect UAC presence. I figured others would benefit from this template as well, so I am sharing it.

Code:
[color #4E9A06]'Allow errors in case the registry key does not exist (Windows XP)[/color]
On Error Resume Next
[color #4E9A06]'Now check if UAC is set.  1=Enabled, 0=Disabled. Missing is same as disabled[/color]
UACPath = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA"
Dim WshShell
Set WshShell = CreateObject("wscript.Shell")
UACValue = WshShell.RegRead(UACPath)
If UACValue = 1 Then
    [color #4E9A06]'Run Elevated[/color]
		If WScript.Arguments.length =0 Then
		  Set objShell = CreateObject("Shell.Application")
		  [color #4E9A06]'Relaunch the script elevated.  
                  'We pass a bogus arg (UAC) to determine if relaunched elevated.[/color]
		  objShell.ShellExecute "wscript.exe", Chr(34) & _
		  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
		  WScript.Quit
		Else 
			Body()
		End If
Else
	Body()
End If

Function Body()
[color #4E9A06]	'Put your script code in here![/color]
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top