0

So I have a powershell script that integrates with several other external third-party EXE utilities. Each one returns its own kind of errors as well as some return non-error related output to stderr (yes badly designed I know, I didn't write these utilities). So What I'm currently doing is parsing the output of each utility and doing some keyword matching. This approach does work but I feel that as I use these scripts and utilties I'll have to add more exceptions to what the error actually is. So I need to create something that is expandable,possibly a kind of structure I can add to an external file like a module.

I was thinking of leveraging the features of a custom PSObject to get this done but I am struggling with the details. Currently my parsing routine for each utility is:

foreach($errtype in {'error','fail','exception'})
{
  if($JobOut -match $errtype){ $Status = 'Failure' }
  else if($JobOut -match 'Warning'){$Status = 'Warning' }
  else { $Status = 'Success' }
}

So this looks pretty straightforward until I run into some utility that contain some of the keywords in $errtype within $JobOut that is not an error. So now I have to add some exceptions to the logic:

foreach($errtype in {'error','fail','exception'})
{
  if($JobOut -match 'error' -and(-not($JobOut -match 'Error Log' }
  elseif($JobOut -match $errtype){ $Status = 'Failure' }
  else if($JobOut -match 'Warning'){$Status = 'Warning' }
  else { $Status = 'Success' }
}

So as you can see this method has the potential to get out of control quickly and I would rather not start editing core code to add a new error rule every time I come across a new error.

Is there a way to maybe create a structure of errors for each utility that contains the logic for what is an error. Something that would be easy to add new rules too?

Any help with this is really appreciated.

4
  • On an unrelated note, {'error','fail','exception'} is a scriptblock which, being a new scope, is relatively costly to create. In this particular case there's no need for the curly braces at all. Commented Feb 25, 2017 at 14:22
  • Ah ok. I spent a lot of time doing shell scripts and for some reason that notation keeps following me. Thanks for the tip. Commented Feb 25, 2017 at 14:26
  • Another tip is following the practices taught by Don Jones. Create a main function with sub-functions that do the work. At the end of each sub-function, since PowerShell's default is all output is captured, use the [PsCustomObject]@{Item1=$Data} format. Then sub-function 2 can check the output from sub-function 1 and continue to process. And so on. Commented Feb 25, 2017 at 16:32
  • Sorry I'm not too familiar with his teachings , I'm a self taught guy and aparantly haven't run across his page until now. I use lots of functions, basically if I have to do something more than once then it should be a function. From what you've written it sounds like you just keep passing the functions output to the next functions input down the line, rinse repeat. Commented Feb 25, 2017 at 18:17

1 Answer 1

1

I would think a switch would do nicely here.

It's very basic, but can be modified very easily and is highly expandable and I like that you can have an action based on the input to the switch, which could be used for logging or remediation.

Create a function that allows you to easily provide input to the switch and then maintain that function with all your error codes, or words, etc. then simply use the function where required.

TechNet Tips on Switches

TechNet Tips on Functions

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

1 Comment

Nice article on the switch statement. Using that might be more maintainable than a bunch of if else statements.

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.