Returning failure from powershell in probes


The new powershell modules in R2 open up new possiblities for MP developers. On a resource utilization standpoint they’re better than Windows Scripting Host and powershell is good for interacting with managed code without having to develop a complete assembly.

For this reason I decided to use a PowerShellPropertyBagProbe for an agent task module, I defined my WriteAction type something like this:


   <WriteActionModuleType ID="Progel.Tasks.WatcherRedir.WAType" Accessibility="Internal" Batching="false">
         <Configuration>
          <xsd:element name="WatcherId" type="xsd:string" />
          <xsd:element name="TaskName" type="xsd:string" />
          <xsd:element name="Overrides" type="xsd:string" />
          <xsd:element name="ScriptTimeout" type="xsd:integer" />
        </Configuration>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <WriteAction ID="ServiceControl" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
                <ScriptName>Progel.Watcher.Restart.WA.ps1</ScriptName>
                <ScriptBody>…ScriptBody>
                        <SnapIns>
                  <SnapIn>Microsoft.EnterpriseManagement.OperationsManager.Client</SnapIn>
                </SnapIns>                <Parameters>
                  <Parameter>
                    <Name>wid</Name>
                    <Value>$Config/WatcherId$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>taskName</Name>
                    <Value>$Config/TaskName$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>overrides</Name>
                    <Value>$Config/Overrides$</Value>
                  </Parameter>
                </Parameters>
                <TimeoutSeconds>$Config/ScriptTimeout$</TimeoutSeconds>
              </WriteAction>
            </MemberModules>

I then coded my powershell script to return a non zero exit code in case of error, so that I could track task failure in console. With my surprise the tasks always ended in success even when they had actually failed. I called for help and Cory (from OpsMgr team) pointed out I was missing a tag in my type definition:

<StrictErrorHandling>true</StrictErrorHandling>

The behavior of this configuration is discussed in the documentation for the other modules (i.e. http://msdn.microsoft.com/en-us/library/ee809362.aspx)  as follows:

The Microsoft.Windows.PowerShellPropertyBagProbe module supports the following configuration parameters:

Parameter

Type

Description

ScriptName

String

Specifies the logical name of the Windows PowerShell script to be run. This value will be used in event logs and debug traces to identify the script.

ScriptBody

String

Contains the Windows PowerShell script to be run.

SnapIns

SnapInsType

Optional. Contains a list of one or more Windows PowerShell snap-ins.

Parameters

String

Specifies a set of Name/Value pairs to be used by the Windows PowerShell script as parameters. This value can be a literal string, a $Target reference, a $MPElement reference, or a $Data reference.

TimeOutSeconds

Integer

Specifies the maximum number of seconds to allow the script to execute before the script is terminated.

StrictErrorHandling

Boolean

Optional. Specifies whether to treat script errors as fatal errors instead of warnings. This should always be set to false in the case of discoveries and should only be true for task workflows, to allow the script error to be raised via the task status. The default value is false.

 

I added the statement to my write action type, but still no luck, even if the task fails it returns in success

image

After reviewing my code finding nothing wrong I tried to throw an exception from powershell using something like throw “BAD”

image

Definitely raw but at least I have a visual return the task has failed.

In conclusion, if you need to check for execution result from a PowerShellWriteAction (or back to the roots from a PowerShellPropertyBagProbe derived module) you must:

  1. set <StrictErrorHandling>true</StrictErrorHandling>
  2. throw an exception from powershell

– Daniele

This posting is provided "AS IS" with no warranties, and confers no rights.

Advertisements
  1. #1 by Niraj on November 9, 2010 - 9:55 am

    did you try using EventPolicy tag in the task xml?

    • #2 by Daniele Grandini on November 12, 2010 - 5:20 pm

      The powershell write action module doesn’t expose the EventPolicy tag, the STrictErrorHandling is supposed to do the trick:

      Microsoft.Windows.PowerShellSchema

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: