vbscript使用Wscript运行powershell脚本-需要从powershell返回



我们在经典ASP页面中使用vbscript,在这个vbscript中,我使用Wscript调用Powershell。我想检查一下返回,因为它的意思是告诉我Powershell是否成功完成。我在Powershell脚本中有一个返回值。objShell我都试过了。运行和objShell。这两个都不允许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脚本是否成功。看看这个例子。

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以外的退出码。

最新更新