Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:31
Show Gist options
  • Save codeartery/1252670806e4b7d8993a38be448f58f0 to your computer and use it in GitHub Desktop.
Save codeartery/1252670806e4b7d8993a38be448f58f0 to your computer and use it in GitHub Desktop.
Will run the current VBScript as an administrator without a UAC prompt after the initial setup run.
REM:<RunAsAdminNoUAC>
' Place this code at the top of your VBScript file you'd like to run with admin permissions.
' When you first run it it will ask for UAC permissions and run and add a task to your Windows 'Task Scheduler' with the name of the script.
' From here on out the VBScript file name and location should not be changed.
' Next time you run it, it will be called from the Task Scheduler with admin permissions and thus not prompt for UAC anymore.
' This script runs with WScript.exe, and does not support command line arguments.
' To delete the task, use the Windows Task Scheduler.
With CreateObject("WScript.Shell")
If WScript.Arguments.Named.Exists("CreateTask") Then
.Run "schtasks /Create /SC ONCE /TN """ & WScript.ScriptName & """ /TR ""wscript.exe \""" & WScript.ScriptFullName & "\"" /AsAdmin"" /ST 00:01 /IT /F /RL HIGHEST", 0, True
ElseIf Not WScript.Arguments.Named.Exists("AsAdmin") Then
If .Run("schtasks /Query /FO CSV /NH /TN """ & WScript.ScriptName & """", 0, True) = 0 Then
.Run "schtasks /Run /TN """ & WScript.ScriptName & """", 0, True
Else
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /CreateTask", "", "runas", 1
End If
WScript.Quit
End If
End With
REM:</RunAsAdminNoUAC>
' Your code here...
REM:<RunAsAdminNoUAC>
With CreateObject("WScript.Shell")
If WScript.Arguments.Named.Exists("CreateTask") Then
.Run "schtasks /Create /SC ONCE /TN """ & WScript.ScriptName & """ /TR ""wscript.exe \""" & WScript.ScriptFullName & "\"" /AsAdmin"" /ST 00:01 /IT /F /RL HIGHEST", 0, True
ElseIf Not WScript.Arguments.Named.Exists("AsAdmin") Then
If .Run("schtasks /Query /FO CSV /NH /TN """ & WScript.ScriptName & """", 0, True) = 0 Then
.Run "schtasks /Run /TN """ & WScript.ScriptName & """", 0, True
Else
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /CreateTask", "", "runas", 1
End If
WScript.Quit
End If
End With
REM:</RunAsAdminNoUAC>
' Example code that needs admin permissions to run.
Dim oWss: Set oWss = CreateObject("WScript.Shell")
Call oWss.Run("cmd /k net user administrator /active:yes")
' Call oWss.Run("cmd /k net user administrator /active:no")
@codeartery
Copy link
Author

You're not supposed to replace the script name. That's a property, it will automatically get your script name.

@Rhemajoe
Copy link

How do I code it to connect to a network

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment