I'm creating Shortcuts using VBScript. My problem though is that I want to create a shortcut that has the "Run as Administrator" flag turned on. I've looked into it and haven't found any option to do so. Am I missing something?
PS: I'm a complete newb to VBScript.
Example Fake Code:
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\ProcessExplorerPortable.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "H:\Documents\PortableApps\ProcessExplorerPortable\ProcessExplorerPortable.exe /t"
oLink.AdminFlag = true
oLink.Save
There are two methods.
The format for shortcuts is an open specification MS-SHLINK. Use CreateShortcut to create a shortcut, then use ADODB as described in this answer Read and Write binary file to modify the flag at byte 21: change bit 6 (0x20) to 1 as described in this answer: Using Power Shell
Just paste an administrative shortcut into your script. This is a run-as-admin shortcut to c:\Windows\Notepad.exe scripted using SFX:
Sub Create_admin_Shortcut(strOutFile)
Dim ts, x
Set ts = CreateObject("Scripting.FileSystemObject").OpenTextFile(strOutFile, 2, True)
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("4C0000000114020000000000C0000000000000468320000020000000B23CE744CDD3C901A81097725EB5D60100D781829E9DC801000E01000000000001000000000000000000000000000000B30014001F50E04FD020EA3A6910A2D808002B30309D19002F433A5C000000000000000000000000000000000000003C003100000000005B5145BA100057494E444F575300260003000400EFBEAD3A61696751B3BA14000000570049004E0044004F00570053000000160048003200000E01008D384F9D20006E6F74657061642E657865002E0003000400EFBEAD3A476A67513ABC140000006E006F00740065007000610064002E0065007800650000001A0000",x,2))) : Next
For x = 1 To 372 Step 2 : ts.Write Chr(Clng("&H" & Mid("00450000001C000000010000001C0000002D00000000000000440000001100000003000000086540FC1000000000433A5C57494E444F57535C6E6F74657061642E657865000060000000030000A05800000000000000626C61636B626F78000000000000000014950DA77200E64181B4C070903D9334ABDD389EB818EB11A7AC0014852134EE14950DA77200E64181B4C070903D9334ABDD389EB818EB11A7AC0014852134EE10000000050000A0240000006900000000000000",x,2))) : Next
ts.Close
End Sub
and modify the script to point to whatever you want:
Create_admin_shortcut(sShortCut)
Set wsh = CreateObject("Wscript.Shell")
set lnk = wsh.CreateShortcut(sShortCut)
lnk.TargetPath = """" & WScript.FullName & """"
lnk.Arguments = """" & WScript.ScriptFullName & """"
lnk.Save
As discussed here, using the file system object to create a file this way is subject to errors in non-English or multi-byte locales. Unfortunately, bytes 4-20 are a CLSID that must not be changed: byte 13 contains the value &HC0 which will fail in some locales. If this is a problem, SHAR does the same encoding using ADODB.