我有一个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
PowerShell代码:
Clear-Host
return 50
我试图保持返回值非常简单,直到它工作。有了这个,我希望MsgBox返回'50',但它会返回'0'。有什么想法我做错了吗?
答案 0 :(得分:6)
我认为您只需要 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
<强> Powershell的强>
exit 50;
编辑#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
<强> Powershell的强>
Write-Output '***SUCCESS***';
答案 1 :(得分:0)
作为后续,使用上面的VBScript输出字符串。在PS脚本中,如果使用例如
write-host "Error: some specific thing happened"
设置各种错误或成功消息
MsgBox executor.StdOut.ReadAll
将显示您通过写主机写的所有内容。
一种向您的VBA用户显示PS脚本结果的有用方法。
仅供参考。