3

I am creating custom forms and adding it to the printer server properties using forms.vbs and running it through cmd. The script is as follows

cscript 'C:\Tools\forms.vbs' -a -n "DD" -u inches -h 7.48 -w 7.48 -t 0 -e 0 -b 7.48 -r 7.48

This works fine when running in command prompt.

Now I invoked this code to the powershell as follows and it also works fine

  $formname = "DD"
  $cmd = "cscript 'C:\Tools\forms.vbs' -a -n " + '"' + $formname + '"' + " -u inches -h 7.48 -w 7.48 -t 0 -e 0 -b 7.48 -r 7.48 "
  Invoke-Expression  $cmd 

The issue started when I thought of checking the error handling thing for the powershell invoke expression.

In cmd when we give the expression as

cscript 'C:\Tools\forms.vbs' -a -n "DD" -u inches -h 7.48 -w 7.48 -t 0 -e 0 -b 0 -r 0

This will definitely throw error as per the notes given in the form.vbs and it will not create a form.

So when I invoked the same error-thrown script to my powershell, the form is not creating as well as it is not throwing any errors. So I request me to guide in this regard. Thanks in advance.

2 Answers 2

3

Invoke-Expression only checks if it's possible to run the command at all. For example, if cscript.exe cannot be found, Invoke-Expression will throw an ObjectNotFound exception.

It doesn't check the exit code of the command or in any way parse its output. You should be able to see the output however.

Make sure you don't mix single and double quotes inside your expression:

$formname = "DD"
# Note double quotes around C:\Tools\forms.vbs
$cmd = 'cscript "C:\Tools\forms.vbs" -a -n ' + '"' + $formname + '"' `
    + ' -u inches -h 7.48 -w 7.48 -t 0 -e 0 -b 7.48 -r 7.48 '
Invoke-Expression $cmd

Output:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Unable to add form DD, error code: 0x1A8. Object required

If you want your code to throw an exception, you need to parse the output manually, e.g.:

try {
    $output = Invoke-Expression $cmd 
    if ($output -like "*error code*") { throw $output }
}
catch [System.Exception] {
    $message = ($Error[0].Exception -split [System.Environment]::NewLine)[-1]
    $message
}

Output:

Unable to add form DD, error code: 0x1A8. Object required
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Alexander, Thanks for the reply. The code to throw the exception is working fine. But still i am not getting any message for Invoke-expression $cmd. like "Unable to add form, error code: 0X1A8".
In my code $output consumes Invoke-Expression output but then you can see it in the exception text. I tested it with PowerShell 2.0.
Am using version 4.0. Does that make any change?
Alexander, When I am running it in ISE, the error is showing up. But when I kept the script under a function and calling it(Powershell Windows Application-using Winforms), the error is not showing up
How can I segregate the out put based on the error?
|
0

I have been using Start-Process to manage installs and uninstalls, and recently started using the same approach for arbitrary executables, which seems to be somewhat what you are trying to do, so...

$new.ExitCode > $null
$filepath = 'cscript.exe'
$argumentList = "C:\Forms.vbs"       
$exitCode = (Start-Process -FilePath:$filePath -argumentList:$argumentList -wait -errorAction:Stop -PassThru).ExitCode
$exitCode

The VBS just throws up a messagebox and quits with a return code, like so.

MsgBox "Text"
WScript.Quit 4

After I close the message box I get that 4 back at the PowerShell console. With no Wscript.Quit, or no exit code provided, I get the expected 0 back. Simple example, but maybe gets you close, assuming you can get the error code you need into a variable so you can return it from the VBS. Or maybe someone points out some nuance I am not aware of and we both learn something. ;)

Comments

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.