3

I am using this way below to create shortcuts on user's desktop. I also want them to run as administrator. I found this, here in StackOverflow but... I want to do the same thing programmatically, not by the hand.

Here is my code:

Private Sub CreateShortcuts()
    Dim NewDir = AppFolder.Text
    Dim WSH As Object = CreateObject("WScript.Shell")
    WSH = CreateObject("WScript.Shell")
    Dim MyShortcut, DesktopPath
    DesktopPath = WSH.SpecialFolders("Desktop")

    MyShortcut = WSH.CreateShortcut(DesktopPath & "\Application Controller.lnk")
    MyShortcut.TargetPath = WSH.ExpandEnvironmentStrings(NewDir & "\Application.exe")
    MyShortcut.WorkingDirectory = WSH.ExpandEnvironmentStrings(NewDir)
    MyShortcut.WindowStyle = 1
    MyShortcut.IconLocation = NewDir & "\Application.exe"
    MyShortcut.Save()
End Sub
Cœur
  • 37,241
  • 25
  • 195
  • 267
Simon
  • 71
  • 1
  • 8

1 Answers1

1

I think you are searching for the 'runas' command. You can say that your command line arguments for the target program is:

runas /savecred /user:administrator "yourprogrampathhere.exe"

Just make sure that the user named 'administrator' does exist. BTW, there is a flag on the Properties window of the shortcut, I just don't know how to do it with the WShell way, but this solution might work. This line needs to change:

 MyShortcut.TargetPath = WSH.ExpandEnvironmentStrings("runas /savecred /user:administrator """ & NewDir & "\Application.exe""")

If you're wondering why I put too many quotation marks, don't worry. They're used to escape the initial quotation mark. Have a wonderful day, and I hope your program will work!

EDIT: Using this will prompt you for an Administrator password. You must have one of that, because empty passwords are not allowed. The password will be saved and won't be prompted again until next system reboot.

ForceMagic
  • 516
  • 8
  • 19
  • Your welcome! :) If you don't want to save the password, make sure you remove the /savecred from the command. – ForceMagic Jul 13 '15 at 17:46