0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried what has been offered, however, when I try to use "WScript.Echo ret" to display the returned value I get the following error: microsoft vbscript runtime error '800a01a8' object required ''
@user3567046 VBScript code is so short that I have no idea what could go wrong. My only guess is that the missing object is oShell and there's something in Set oShell = WScript.CreateObject ("WScript.Shell") Maybe a typo? Please check your code carefully or supply a complete command and code listing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.