如何将PowerShell变量返回给VBScrip [英] How to return PowerShell variable to VBScript

查看:0
本文介绍了如何将PowerShell变量返回给VBScrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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***';

这篇关于如何将PowerShell变量返回给VBScrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆