Skip to content

Instantly share code, notes, and snippets.

@Artoria2e5
Created March 18, 2020 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Artoria2e5/0cfc88ba98e48a643b1d92568491e368 to your computer and use it in GitHub Desktop.
Save Artoria2e5/0cfc88ba98e48a643b1d92568491e368 to your computer and use it in GitHub Desktop.
Proxy script for running anything as an admin. Contains an CommandLineToArgvW escaper.
' Proxy script for running anything as an admin. In this case, AHK.
Set UAC = CreateObject("Shell.Application")
Dim Arguments
' Build arguments
Arguments = ""
For Each strArg in WScript.Arguments
Arguments = Arguments & Escape(strArg) & " "
Next
UAC.ShellExecute "C:\Program Files\AutoHotkey\AutoHotkey.exe", Arguments, "", "runas", 1
' Escape per CommandLineToArgv.
' See https://github.com/Artoria2e5/node/blob/fix!/child-process-args/doc/api/child_process.md#windows-command-line
' See https://github.com/nodejs/node/pull/29576
Function Escape(Arg)
' js: return `"${arg.replace(/(\\*)($|")/g, '$1$1$2').replace(/"/g, '""')}"`;
Dim tmp
Set sub1 = New RegExp
sub1.Global = True
sub1.pattern = "(\\*)($|"")"
tmp = sub1.Replace(Arg, "$1$1$2")
Set sub2 = New RegExp
sub2.Global = True
sub2.pattern = Chr(34)
tmp = sub2.Replace(tmp, Chr(34) & Chr(34))
Escape = Chr(34) & tmp & Chr(34)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment