如何将PowerShell变量返回到VBScript

Qua*_*nda 4 vbscript powershell hta

我有一个vbscript来调用PowerShell脚本,希望将PowerShell输出返回到HTA(HTML应用程序)GUI.现在我只想看看我是否可以将PowerShell输出返回到vbscript中的MsgBox.我没有太多运气.

VBScript代码:

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

PowerShell代码:

Clear-Host
return 50
Run Code Online (Sandbox Code Playgroud)

我试图保持返回值非常简单,直到它工作.有了这个,我希望MsgBox返回'50',但它会返回'0'.什么想法我做错了什么?

Rob*_*Dee 8

我想你只想让exit命令获取返回值:

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
Run Code Online (Sandbox Code Playgroud)

电源外壳

exit 50;
Run Code Online (Sandbox Code Playgroud)

编辑#1

或者如果你想获取一个返回字符串:

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
Run Code Online (Sandbox Code Playgroud)

电源外壳

Write-Output '***SUCCESS***';
Run Code Online (Sandbox Code Playgroud)