【问题标题】:vbscript using Wscript to run a powershell script - need the return from powershellvbscript 使用 Wscript 运行 powershell 脚本 - 需要从 powershell 返回
【发布时间】:2015-08-27 17:29:35
【问题描述】:

我们在经典 ASP 页面中使用 vbscript,在该 vbscript 中,我使用 Wscript 调用 Powershell。我想检查返回,因为它是为了告诉我 Powershell 是否成功完成。我在 Powershell 脚本中有一个返回值。我已经尝试了 objShell.Run 和 objShell.Exec,但没有一个允许 Powershell 返回值通过我的 ASP 页面。

我的问题:如何从 Powershell 获取返回值?

VBScript 如下:

'call PowerShell script with filename and printername and scriptname
strScript = Application("EnvSvcsPSScript")
Set objShell = CreateObject("Wscript.Shell") 
dim strCommand
strCommand = "powershell.exe -file " & strScript & " " & strFileName & " " & strPrinterName
Set strPSReturn = objShell.Run(strCommand, 0, true)

response.Write("return from shell: " & strPSReturn.StdOut.ReadAll & "<br>")
response.Write("return from shell: " & strPSReturn.StdErr.ReadAll & "<br>")

Powershell 脚本:

$FileName = $args[0]
$PrinterName = $args[1]
$strReturn = "0^Successful"

"Filename: " + $FileName
"Printer:  " + $PrinterName

try
{
get-content $FileName | out-printer -name $PrinterName
[gc]::collect() 
[gc]::WaitForPendingFinalizers()
}
catch
{
    $strReturn = "1^Error attempting to print report."
}
finally
{

}
return $strReturn

谢谢!

【问题讨论】:

    标签: powershell vbscript wsh


    【解决方案1】:

    您可以检查您的 PowerShell 脚本是否成功。看看这个例子。

    Powershell 脚本:

    $exitcode = 0
    try
    {
        # Do some stuff here
    }
    catch
    {
        # Deal with errors here
        $exitcode = 1
    }
    finally
    {
        # Final work here
        exit $exitcode
    }
    

    VB 脚本:

    Dim oShell
    Set oShell = WScript.CreateObject ("WScript.Shell")
    Dim ret
    ret = oShell.Run("powershell.exe -ep bypass .\check.ps1", 0, true)
    WScript.Echo ret
    Set oShell = Nothing
    

    现在,如果您运行 VB 脚本,如果 PowerShell 脚本成功,您将获得 0,否则为 1。 但是,这种方法不会让您获得 0 或 1 以外的退出代码。

    【讨论】:

    • 尝试了提供的内容,但是,当我尝试使用“WScript.Echo ret”显示返回值时,我收到以下错误:microsoft vbscript runtime error '800a01a8' object required ''
    • @user3567046 VBScript 代码太短了,我不知道会出什么问题。我唯一的猜测是丢失的对象是oShellSet oShell = WScript.CreateObject ("WScript.Shell") 中有一些东西可能是错字?请仔细检查您的代码或提供完整的命令和代码清单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2019-12-02
    • 2019-09-30
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多