如何将PowerShell变量返回给VBScrip

人气:270 发布:2022-10-16 标签: powershell vbscript hta

问题描述

我有一个VB脚本可以调用PowerShell脚本,希望将PowerShell输出返回到HTA(HTML应用程序)图形用户界面。现在,我只想看看是否可以将PowerShell输出返回到VB脚本中的MsgBox中。这件事我运气不太好。

VBScript代码:

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

PowerShell代码:

Clear-Host
return 50

我尽量保持返回值非常简单,直到它起作用。这样,我预计MsgBox将返回‘50’,但它返回的是‘0’。你知道我做错了什么吗?

推荐答案

我认为您只希望Exit命令获得返回值:

VBScrip

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;

编辑#1

或者如果您想获取回车字符串:

VBScrip

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***';

292