Ciprian Dragoe
Ciprian Dragoe

Reputation: 339

vbs run program as admin and with a priority level

I want on startup to launch a vbs script that will launch another program as administrator and set the priority of that program to above normal or high priority.

I currently have made it to launch the program as admin but am stuck on setting the process level.

Set app = CreateObject("Shell.Application")
app.ShellExecute """d:\SYNC\Dropbox\PORTABLE_PROGRAMS\ahk\Navigare\KeyboardEnchancer\KeyboardEnchancer.exe""", , , "runas", 3

Upvotes: 0

Views: 1284

Answers (3)

softwearmale
softwearmale

Reputation: 1

This answers the question... as admin with priority.

dim sFile: sFile=chr(34)& "C:\Windows\System32\notepad.exe" &chr(34)
CreateObject("Shell.Application").ShellExecute "cmd.exe","/c START /HIGH /B """" " &sFile, ,"runas", 1

Upvotes: 0

softwearmale
softwearmale

Reputation: 1

I used this to change a script's own priority from within its execution:

Sub SetPriority()
    Const IDLE = 64, BELOW_NORMAL = 16384, NORMAL = 32, ABOVE_NORMAL = 32768, HIGH_PRIORITY = 128, REAL_TIME = 256
    GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("Select * From Win32_Process Where CommandLine Like '%" &Wscript.ScriptName& "%'").ItemIndex(0).SetPriority(HIGH_PRIORITY)
End Sub

Call SetPriority()

Upvotes: 0

user10750522
user10750522

Reputation:

I edited the answer to address your permission problem, the script now self-elevates to run as administrator, more info at: How to Automatically Elevate a Vbscript to Run it as Administrator?. Tested and working flawlessly on my machine.

If WScript.Arguments.length = 0 Then

  Set objShell = CreateObject("Shell.Application")
   'Pass a bogus argument, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
    WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1

Else

    Set objShell= CreateObject("Shell.Application")

    strComputer = "."
    Const HIGH_PRIORITY = 128
    processName = "notepad.exe"   ' The process name of your app
    appName = "C:\Windows\System32\notepad.exe" ' The app you want to run

    objShell.ShellExecute appName, , , "runas", 1

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colProcesses = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = '" & processName & "'")

    For Each p in colProcesses  
        p.SetPriority(HIGH_PRIORITY)
    Next

End If

More info at: SetPriority method of the Win32_Process class and ShellExecute method.

Upvotes: 2

Related Questions