1

I have a simple powershell script that calls a perl script. when the perl script fails, I would like to catch the error and put it into a variable.

The following actually works when I run it from powergui:

try
{

$erroractionpreference = "Stop"
perlexe c:\perlscript.pl -perlparameters param1
}
catch
{
 $perlerror = $_.Exception.Message; 

}

but when I run it from the powershell command prompt the $perlerror variable is not populated with the error message.

anyone knows why? is there a better way that I can catch perl errors?

1 Answers1

1

The try...catch statement handles only exceptions within Powershell context. Perlexe runs a Perl script and any exceptions being rised in the Perl part are handled within Perlexe's process. No exception will bubble back to the Powershell. All you can get are textual error messages printed on either stdout or stderr.

In order to capture stdout and stderr, use the syntax that ikegami linked into. Like so,

$cmdOutput = perlexe c:\perlscript.pl -perlparameters param1 2>&1
vonPryz
  • 22,996
  • 7
  • 54
  • 65