2016-04-29 46 views
3

我有一個vbscript調用PowerShell腳本,希望將PowerShell輸出返回到HTA(HTML應用程序)GUI。現在我只想看看是否可以將PowerShell輸出返回到VBScript中的MsgBox。我沒有太多運氣。如何將PowerShell變量返回給VBScript

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'。任何想法我做錯了什麼?

+1

我認爲0是PowerShell執行的退出代碼,代表成功。似乎你的'return'拉到了執行狀態。你可以在PowerShell中退出50,但我不認爲這是做事的合法方式。 –

+0

你可以看看這個==> [在HTA中獲取PowerShell腳本的輸出](http://stackoverflow.com/questions/35198533/get-output-of-a-powershell-script-in-a -hta/35267951#35267951) – Hackoo

回答

2

我覺得你只是想出口命令得到的返回值:

的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***'; 
+0

甜,它得到它返回50!現在這隻適用於數值嗎?我將出口更改爲字符串值並且不起作用 – Quanda

+1

退出僅支持整數返回值,但您可以只抓取輸出。我已經擴展了我的答案來演示。 –

相關問題