2015-08-27 199 views
0

我们在经典ASP页面中使用vbscript,并在该vbscript中使用Wscript调用Powershell。我想检查回报,因为它是为了告诉我Powershell是否成功完成。我在Powershell脚本中有一个返回值。我已经尝试了objShell.Run和objShell.Exec,并且都没有让Powershell返回值通过我的ASP页面。使用Wscript运行powershell脚本的vbscript - 需要从powershell返回

我的问题:如何从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 

THANK YOU!

回答

0

您可以检查您的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脚本,你会得到0,如果PowerShell脚本成功,否则为1。 但是,这种方法不会让你得到0或1以外的退出代码。

+0

尝试了什么已提供,但是,当我尝试使用“WScript.Echo ret”显示返回值时出现以下错误:microsoft vbscript运行时错误'800a01a8'所需的对象'' – LReeder14

+0

@ user3567046 VBScript代码太短,我不知道会出现什么问题。 我唯一的猜测是缺少的对象是'oShell',并且'Set oShell = WScript.CreateObject(“WScript.Shell”)中有一些''也许是一个错字? 请仔细检查您的代码或提供完整的命令和代码清单。 – Emons