Powershell module doesn’t obey optional parameters


When I stuck into this issue it took a couple of hours to understand what was going wrong in my management pack.

Let’s start from the error I got:

Log Name: Operations Manager

Source: HealthService

Date: 11/16/2011 4:48:25 PM

Event ID: 4509

Task Category: Health Service

Level: Error

Keywords: Classic

User: N/A

Computer:

Description:

The constructor for the managed module type "Microsoft.EnterpriseManagement.Modules.PowerShell.PowerShellProbeActionModule" threw an exception. This module was running in rule "Progel.Biztalk.2009.ServiceInstances.Orchestration.Dehidrated.Monitor" running for instance "…" with id:"{CDDF9D07-4144-1CCF-2C9E-9ED867B691C9}" in management group "…".

The exception text is:

Microsoft.EnterpriseManagement.HealthService.ModuleException: Failed to parse module configuration, please see inner exception for details. —> System.Xml.XmlException: ‘EndElement’ is an invalid XmlNodeType. Line 418, position 344.

at System.Xml.XmlReader.ReadElementString(String name)

at Microsoft.EnterpriseManagement.Modules.PowerShell.PowerShellProbeActionModule.ReadParametersXml(XmlReader reader)

at Microsoft.EnterpriseManagement.Modules.PowerShell.PowerShellProbeActionModule..ctor(ModuleHost`1 moduleHost, XmlReader configuration, Byte[] previousState)

— End of inner exception stack trace —

at Microsoft.EnterpriseManagement.Modules.PowerShell.PowerShellProbeActionModule..ctor(ModuleHost`1 moduleHost, XmlReader configuration, Byte[] previousState)

 

My monitor used a datasource composed with a powershell probe and a simple scheduler. Some parameters were marked as optional (highlighted).

<ProbeActionModuleType ID="Progel.Biztalk.2009.ServiceInstances.PT" Accessibility="Public" Batching="false">
  <Configuration>
    <xsd:element name="TraceLevel" type="xsd:string" =""/>               <xsd:element name="ComputerName" type="xsd:string" =""/>
    <xsd:element name="IntervalHours" type="xsd:integer" =""/>         <xsd:element name="StatRowsLimit" type="xsd:integer" =""/>       <xsd:element name="ScriptTimeout" type="xsd:integer" =""/>
    <xsd:element name="ErrorIDRegEx" type="xsd:string" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="ErrorDescriptionRegEx" type="xsd:string" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="StrictErrorHandling" type="xsd:boolean" minOccurs="0" maxOccurs="1"/>
  </Configuration>

obvisously the powershell probe took into account the optional parameters:

<ProbeAction ID="Probe" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>Progel.Biz2009.ServiceInstace.DS.ps1</ScriptName>
<ScriptBody>
<![CDATA[
# Get the named parameters
param([int]$traceLevel=$(throw 'must have a value'),
[string]$computerName=$(throw 'must have a value'),
[int]$intervalHours=$(throw 'must have a value'),
[int]$statRowsLimit=$(throw 'must have a value'),
[string]$regExErrorID=$null,
[string]$regExErrorDesc=$null)
... 
]]>
 </ScriptBody>

The datasource had a similar signature:

            <DataSourceModuleType ID="Progel.Biztalk.2009.ServiceInstances.DS" Accessibility="Public" Batching="false">                 <Configuration>                     <xsd:element name="TraceLevel" type="xsd:integer" =""/>                     <xsd:element name="ComputerName" type="xsd:string" =""/>                     <xsd:element name="IntervalHours" type="xsd:integer" =""/>                      <xsd:element name="StatRowsLimit" type="xsd:integer" =""/>                      <xsd:element name="ScriptTimeout" type="xsd:integer" =""/>                     <xsd:element name="IntervalSeconds" type="xsd:integer" =""/>                     <xsd:element name="SyncTime" type="xsd:string" =""/>                     <xsd:element name="ErrorIDRegEx" type="xsd:string" minOccurs="0" maxOccurs="1"/>                     <xsd:element name="ErrorDescriptionRegEx" type="xsd:string" minOccurs="0" maxOccurs="1"/>
              </Configuration>

Finally the monitor type used the datasource, omitting the two optional parameters:

        <MonitorImplementation>

          <MemberModules>

            <DataSource ID="Messages" TypeID="Progel.Biztalk.2009.ServiceInstances.DS">

              <TraceLevel>$Config/TraceLevel$</TraceLevel>

              <ComputerName>$Config/ComputerName$</ComputerName>

              <IntervalHours>$Config/IntervalHours$</IntervalHours>

              <StatRowsLimit>$Config/StatRowsLimit$</StatRowsLimit>

              <ScriptTimeout>$Config/ScriptTimeout$</ScriptTimeout>

              <IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>

              <SyncTime>$Config/SyncTime$</SyncTime>

            </DataSource>

This composition is perfectly legal and in fact I can verify and seal my management pack, the error popped out when the monitor was executed. The exception told me there was an error in module configuration (Failed to parse module configuration, please see inner exception for details.) and that it somehow reaches the end of the XML snippet too early (‘EndElement’ is an invalid XmlNodeType). Now that I know the solution it’s an easy guess, but I must admit I didn’t immediately get to the culprit, I first started enclosing my parameters in CDATA tags thinking something was wrong with them and only lastly I added the optional parameters to the monitor. This did the trick, I then performed all the regression tests I could think of and the conclusion is the PowershellProbeAction Module doesn’t consider optional parameters. The solution is easy: don’t use them, always specify all the parameters listed in the configuration section.

– Daniele

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

Advertisements
  1. #1 by Haimiko on August 1, 2015 - 12:47 am

    Ran into the same issue. Thanks for the ip.

  2. #2 by ernestbrant on March 30, 2012 - 10:27 pm

    Thanks very much for this tip Daniele :)
    Ernie

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: