ElevateWscript.vbs

'=========================================================================
' ElevateWscript.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: [email protected]
' WEB: https://sigkillit.com
' DATE: 1/25/2012
' COMPATIBLE: Windows Vista, Server 2008, and Above
' COMMENTS: Since the introduction of UAC in Windows, despite being an
' administrator you may still need to run a script with elevated
' privileges. For example the CommandLine property of the WIN32_Process class
' requires a script to be elevated in order to return valid data when the process
' is running as another user. Unfortunately, Windows does not provide Run As
' Administrator on the context of a .vbs file, which is why I made this.
' To use the script, pass the full path to a .vbs file to the Sub and it
' will prompt you to run the script elevated. If the script you need to elevate
' in located in the same directory as this script, you can just pass the script
' name. Alternatively, you can always launch your vbscript from an elevated
' command prompt which will use wscript or cscript elevated as well.
' EXAMPLE: Elevate a script using the full path
'          Call ElevateWscript("C:\scripts\test.vbs")
' EXAMPLE: Elevate a script in the same directory as ElevateWscript.vbs
'          Call ElevateWscript("test.vbs")
'=========================================================================
Option Explicit
Call ElevateWscript("test.vbs")

Private Sub ElevateWscript(scriptName)
    Dim objShell: Set objShell = CreateObject("Shell.Application")
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(scriptName) Then
        Dim objFile: Set objFile = objFSO.GetFile(scriptName)
         objShell.ShellExecute "wscript.exe", Chr(34) & objFile.Path & Chr(34), "", "runas", 1
    Else
         Wscript.Echo "Script Does Not Exist!" & vbCrLf & scriptName
    End If
End Sub

 

2 comments

1 ping

    • Janae Bailey on April 27, 2015 at 2:54 pm

    I tried using the script and when I run it on my account (I have administrator access) it works fine, calls my script and everything is fine. When I run it on an account that doesn’t have administrator access it gives me an error saying I do not have Active Directory access. I have tried so many different scripts and variations to get this to work but I am having no luck and was wondering if you could help me. TIA.

    1. If your script is accessing Active Directory, the account running the script needs permissions to your Active Directory. If the user account does not have permissions to active directory, you must use “RunAs” to run the script as a user that has access such as the domain admin.

      This script is designed for issues running scripts on a local computer that has User Account Control enabled. I explain all of this here:
      https://sigkillit.com/2013/01/26/running-vbscripts-with-uac-elevation/

  1. […] ← Previous […]

Comments have been disabled.