Basati Naveen
Basati Naveen

Reputation: 323

invoke-expression doesnot throw an error in powershell

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.

Upvotes: 3

Views: 6418

Answers (2)

Alexander Obersht
Alexander Obersht

Reputation: 3275

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

Upvotes: 3

Gordon
Gordon

Reputation: 6883

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. ;)

Upvotes: 0

Related Questions