How to return PowerShell variable to VBScript

10,440

I think you just want the exit command to get the return value:

VBScript

pscommand = ".\myscript.ps1; exit $LASTEXITCODE"
cmd = "powershell.exe -noprofile -command " & pscommand
Set shell = CreateObject("WScript.Shell")
rv = shell.Run(cmd, , True)
MsgBox "PowerShell returned: " & rv, vbSystemModal

Powershell

exit 50;

EDIT #1

Or if you want to grab a return string:

VBScript

pscommand = ".\myscript2.ps1"
cmd = "powershell.exe -noprofile -command " & pscommand
Set shell = CreateObject("WScript.Shell")
Set executor = shell.Exec(cmd)
executor.StdIn.Close
MsgBox executor.StdOut.ReadAll

Powershell

Write-Output '***SUCCESS***';
Share:
10,440
Eric Furspan
Author by

Eric Furspan

Frontend developer with 5+ years of experience working with various web technologies in developing high quality applications and services.

Updated on June 08, 2022

Comments

  • Eric Furspan
    Eric Furspan about 2 years

    I have a vbscript to call a PowerShell script in hopes of returning the PowerShell output to an HTA (HTML Application) GUI. Right now I just want to see if I can return the PowerShell output into a MsgBox in the vbscript. I am not having much luck with this.

    VBScript Code:

    Set shell = CreateObject("WScript.Shell")
    return = shell.Run("powershell.exe -executionpolicy bypass -noprofile -file pathToScript\PowerShellToVBA.ps1", , true)
    MsgBox return
    

    PowerShell Code:

    Clear-Host
    return 50
    

    I am trying to keep the return value extremely simple until it works. With this, I would expect the MsgBox to return '50', however it is returning '0' instead. Any ideas what im doing wrong?