2017-06-22 29 views
1

是否可以使用VBScript來運行Powershell代碼(不是.ps1文件)? 例如,要在VBScript下調用Powershell函數(此腳本必須集成在VBScript代碼中)。 如何使用VBScript執行外部.ps1腳本我知道,但我沒有找到任何有關集成的信息。 任何想法? 謝謝是否可以從VBScript運行Powershell代碼?

+1

不,這不能完成,因爲VBScript解釋器無法處理PS代碼。難道你真正的問題是從你的VBScript傳遞參數值到你的外部PS腳本? – Filburt

+0

謝謝你的回答。不,我不想將參數傳遞給我的外部ps腳本。主要想法是使用一個腳本執行多個任務,但它應該是可讀的(不是.exe)。 –

+0

您可以從VBS中調用「powershell -command」,技術上可以在腳本中以字符串的形式創建該命令。 – Vesper

回答

2

Filburt已經指出,VBScript無法執行Powershell。但是,您可以執行的操作是啓動Powershell並將腳本作爲參數傳遞。像這樣,

option explicit 

Const WshRunning = 0 
Const WshFinished = 1 
Const WshFailed = 2 

Dim objShell, oExec, strOutput, strPS1Cmd 

' Store the ps1 code in a variable 
strPS1Cmd = "& { get-date }" 

' Create a shell and execute powershell, pass the script  
Set objShell = wscript.createobject("wscript.shell") 
Set oExec = objShell.Exec("powershell -command """ & strPS1Cmd & """ ") 

Do While oExec.Status = WshRunning 
    WScript.Sleep 100 
Loop 

Select Case oExec.Status 
    Case WshFinished 
     strOutput = oExec.StdOut.ReadAll() 
    Case WshFailed 
     strOutput = oExec.StdErr.ReadAll() 
End Select 

WScript.Echo(strOutput) 

在複雜的參數情況下,可以考慮使用接受Base64編碼命令-EncodedCommand。方便地解決報價逃脫等問題。

+0

下運行,但如果我有PowerShell代碼爲60行,我可以通過它的參數? –

+0

@VictorVasilyonok至少你可以通過標準輸入扔在指揮和管你60行單破折號PowerShell的。同樣在編碼命令的情況下,答案是正確的。 – Vesper

+0

@VictorVasilyonok VBScript可以將PS1文件寫入磁盤,然後運行它。如何將實際腳本存儲在集中式網絡共享上並使用VBScript來複制和/或執行這些腳本? – vonPryz

相關問題