0
votes

We are using vbscript in a Classic ASP page and in that vbscript I am calling Powershell using Wscript. I would like to check the return as it is meant to tell me whether the Powershell finished successfully of not. I have a return value in the Powershell script. I've tried both objShell.Run and objShell.Exec and neither one is allowing the Powershell return value through to my ASP page.

My question: how can I get the return values from Powershell?

VBScript follows:

'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 script:

$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!

1

1 Answers

0
votes

You can check whether your PowerShell script succeeded. Check out this example.

Powershell script:

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

VB Script:

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

Now if you run the VB script you'll get 0 if the PowerShell script succeeded and 1 otherwise. However this approach won't let you get exit codes other than 0 or 1.