Search code examples
batch-filecommand-linevbscriptbatch-processing

Run script as administrator passing arguments


I am trying to run one command as administrator but I am getting error. Below is the code.

VBScript:

MyPath ="C:\Destination"
Dim objShell
Set objShell = CreateObject("Shell.Application")  
objShell.ShellExecute "C:\batchScript.cmd " & MyPath &, "", "", "runas", 1

batchScript.cmd:

echo %1
psfile %1 -c

Solution

  • Wild guess (since you didn't deem it necessary to actually show the error message): you're using the concatenation operator (&) without something to actually concatenate:

    objShell.ShellExecute "C:\batchScript.cmd " & MyPath &, "", "", "runas", 1
    '                                                    ^
    

    and you're also passing arguments as part of the command.

    Remove the spurious concatenation operator and pass the argument as an actual argument:

    objShell.ShellExecute "C:\batchScript.cmd", MyPath, "", "runas", 1
    

    Also, $args[0] is not a valid variable in a batch file.