' =========
' EnableLUA
' =========
' Version 1.0.0.5 - June 15th 2022
' Copyright © Steve MacGuire 2011-2022
' http://samsoft.org.uk/iTunes/EnableLUA.vbs
' Please visit http://samsoft.org.uk/iTunes/scripts.asp for updates

' =======
' Licence
' =======
' This program is free software: you can redistribute it and/or modify it under the terms
' of the GNU General Public License as published by the Free Software Foundation, either
' version 3 of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the GNU General Public License for more details.

' Please visit http://www.gnu.org/licenses/gpl-3.0-standalone.html to view the GNU GPLv3 licence.

' ===========
' Description
' ===========
' Displays and optional changes the state of the Windows EnableLUA policy setting

' =========
' ChangeLog 
' =========
' Version 1.0.0.1 - Initial version
' Version 1.0.0.2 - Check setting has been applied and warn if not
' Version 1.0.0.3 - Change reboot warning since it doesn't seem to be required
' Version 1.0.0.4 - Elevate script
' Version 1.0.0.5 - Update prompts


' ==========
' To-do List
' ==========
' Add things to do

' =============================
' Declare constants & variables
' =============================
Option Explicit	        ' Declare all variables before use
Dim nl,tab              ' New line/tab strings
Dim Title,Summary       ' Text for dialog boxes
nl=vbCrLf
tab=Chr(9)
Title="EnableLUA"
Summary="Change the state of the Windows EnableLUA policy setting." & nl & nl & _
  "When this setting is DISABLED scripts from samsoft.org.uk can display " & _
  "a progress bar while working. Note however this reduces the built-in " & _
  "security of Windows and, with Windows 8 or later, prevents Metro apps from " & _
  "working, so you may want to ENABLE this property later."

' ============
' Main program
' ============

Elevate                 ' Ensure script runs with elevated permissions
ChangeLUA               ' Main process 

' ===================
' End of main program
' ===================


' ===============================
' Declare subroutines & functions
' ===============================  


' Report and change setting if required
' Modified 2022-06-15
Sub ChangeLUA
  Dim M,R,U
  U=UAC
  M=Summary & nl & nl & "The current value of EnableLUA is " & Enabled(U) & "."
  M=M & nl & nl & "Would you like to change it?"
  R=MsgBox(M,vbYesNo+vbQuestion,Title)
  If R=vbYes Then
    If U Then
      SetLUA 0
    Else
      SetLUA 1
    End If
    If U=UAC Then
      M="The script was unable to change the state. You may need to edit the registry manually or edit the permissions of the branch "
      M=M & "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System so that the script can edit the EnableLUA value."
    Else
      M="The current value of EnableLUA is " & Enabled(UAC) & "." & nl & nl
      M=M & "You may need to reboot your computer for the change to take effect, "
			M=M & "however it seems a reboot isn't required for the purpose of enabling or disabling the progress bars."
    End If
    MsgBox M,vbExclamation,Title
  End If    
End Sub


' Run script with elevated permissions if required
' Modified 2016-01-19
Sub Elevate
  Dim Shell
  If WScript.Arguments.length=0 Then
    ' MsgBox "Elevating script"
    Set Shell=CreateObject("Shell.Application")
    Shell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " uac", "", "runas", 1
    WScript.Quit        ' Otherwise we'll run the script twice
  Else
    ' MsgBox "Running elevated"
  End If
End Sub


' Return status label
' Modified 2014-04-18
Function Enabled(V)
  If V Then Enabled="ENABLED" Else Enabled="DISABLED"
End Function


' Set value of EnableLUA 
' Modified 2014-04-18
Sub SetLUA(Value)
  Dim Reg
  Const HKEY_LOCAL_MACHINE=&H80000002
  Const KeyPath="Software\Microsoft\Windows\CurrentVersion\Policies\System"
  Const KeyName="EnableLUA"
  Set Reg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") 	  ' Use . for local computer, otherwise could be computer name or IP address
  Reg.SetDWORDValue HKEY_LOCAL_MACHINE,KeyPath,KeyName,Value
End Sub

  
' Detect if User Access Control is enabled, UAC (or rather LUA) prevents use of progress bar
' Modified 2011-10-18
Function UAC
  Const HKEY_LOCAL_MACHINE=&H80000002
  Const KeyPath="Software\Microsoft\Windows\CurrentVersion\Policies\System"
  Const KeyName="EnableLUA"
  Dim Reg,Value
  Set Reg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") 	  ' Use . for local computer, otherwise could be computer name or IP address
  Reg.GetDWORDValue HKEY_LOCAL_MACHINE,KeyPath,KeyName,Value	  ' Get current property
  If IsNull(Value) Then UAC=False Else UAC=(Value<>0)
End Function


' Wrap & tab long strings, break string S on first separator C found at or before character W adding T tabs to each new line
' Modified 2014-05-29
Function Wrap(S,W,C,T)
  Dim P,Q
  P=InstrRev(S," ",W)
  Q=InstrRev(S,"\",W)
  If Q>P Then P=Q
  If P Then
    Wrap=Left(S,P) & nl & String(T,tab) & Wrap(Mid(S,P+1),W,C,T)
  Else
    Wrap=S
  End If
End Function


' ==============
' End of listing
' ==============