Hello,
does someone know a good way to parse the exception thrown by an executed powershell script?
All i get back is a string and then i must parse the output somehow.
What i would like to have back is an object, like an "PowerShell:PowerShellRemotePSObject".
Forced to parse the exception string i have no unique character to make a split for.
Example output:
'PowerShellInvocationError: Errors found while executing script
System.Management.Automation.RuntimeException: No VM with Name bla.foo.com found
(Dynamic Script Module name : invokeScript#14)'
If i would split for ":" or "(" then i am not able to use those characters in the powershell scripts anymore.
Regards,
Marcus
In the meantime i built this little snippet:
/*
Important for the error handling. Please surround every command with a try catch and return in the catch area only "Throw $PSItem.Exception.Message".
With this is possible to parse the message way better.
Example:
PowerShellInvocationError: Errors found while executing script
System.Management.Automation.RuntimeException: Failed to generate proxies for remote module 'virtualmachinemanager.R2Aliases'. Running the Get-Command command in a remote session returned no results.
(Dynamic Script Module name : invokeScript#14)
The first line is always the same so easy to elminiate.
The second line is the message we want to have, but we loo for the first occurance of the colon.
The third line changes the number, so we look for the first bracket.
We make a substring between the first colon and the last bracket.
Trimming in the end is always a good idea and as a result we have the string we want to have.
*/
var parsedErrorMessage = errorCode.replace("PowerShellInvocationError: Errors found while executing script","");
var positionOfBracket = parsedErrorMessage.lastIndexOf("(");
var positionOfColon = (parsedErrorMessage.indexOf(":"))+1;
var parsedErrorMessage = parsedErrorMessage.substring(positionOfColon,positionOfBracket);
var parsedErrorMessage = parsedErrorMessage.trim();
throw parsedErrorMessage;
I Improved the script and directly altered the workflow "Invoke an external script" added a exception to the scripted task with the following code:
/*
Important for the error handling. Please surround every command with a try catch and return in the catch area only "Throw $PSItem.Exception.Message".
With this it is possible to parse the message way better.
Example:
PowerShellInvocationError: Errors found while executing script
System.Management.Automation.RuntimeException: Failed to generate proxies for remote module 'virtualmachinemanager.R2Aliases'. Running the Get-Command command in a remote session returned no results.
(Dynamic Script Module name : invokeScript#14)
The first line is always the same so easy to elminiate.
The second line is the message we want to have, but we loo for the first occurance of the colon.
The third line changes the number, so we look for the first bracket.
We make a substring between the first colon and the last bracket.
Trimming in the end is always a good idea and as a result we have the string we want to have.
*/
var parsedErrorMessage = errorCode.replace("PowerShellInvocationError: Errors found while executing script","");
var positionOfBracket = parsedErrorMessage.lastIndexOf("(");
var positionOfColon = (parsedErrorMessage.indexOf(":"))+1;
var parsedErrorMessage = parsedErrorMessage.substring(positionOfColon,positionOfBracket);
var parsedErrorMessage = parsedErrorMessage.trim();
//throw parsedErrorMessage;// <-- If this step throws the error, then this is always the location where it fails and makes finding the source of the error very hard. So we only reset the variable errorCode to have the caller workflow be the source of the error and not this slave workflow
errorCode = parsedErrorMessage;
From there both arrows, fail and success, go to "throw Exception".