powershellcmdvbscriptquotingprocess-elevation

Launching a batch file with elevation via a PowerShell command from VBS


any idea to fix the following that I have with my script?

Set objShell = CreateObject("Wscript.Shell")
 objShell.Run("powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c "C:\Temp\CAL.bat"'"")

Error: Expected ')'

I took the idea from Running Powershell from vbs with command as parameter, but my little experience with these things does not help me.


Solution

  • Therefore:

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "powershell -Command ""Start-Process cmd -Verb RunAs -ArgumentList '/c \""C:\Temp\CAL.bat\""'"""
    

    Note:


    If you want the entire invocation to run invisibly, there are two aspects:

    Therefore:

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "powershell -Command Start-Process C:\Temp\CAL.bat -Verb RunAs -WindowStyle Hidden", 0
    

    Separately, if you want the processes to run synchronously, i.e. if you want the object.Run() call to wait until all launched processes have finished:

    Therefore, if you want to combine invisible and synchronous execution, as well as capture the exit code:

    Set objShell = CreateObject("Wscript.Shell")
    exitCode = objShell.Run("powershell -Command exit (Start-Process C:\Temp\CAL.bat -Verb RunAs -WindowStyle Hidden -Wait -PassThru).ExitCode", 0, true)