<ManagementPack ContentReadable="true" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <Manifest>
    <Identity>
      <ID>code4ward.Sample.PowerShellMonitor</ID>
      <Version>1.0.1.0</Version>
    </Identity>
    <Name>code4ward.Sample.PowerShellMonitor</Name>
    <References>
      <Reference Alias="SC">
        <ID>Microsoft.SystemCenter.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="Windows">
        <ID>Microsoft.Windows.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="Health">
        <ID>System.Health.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="System">
        <ID>System.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
    </References>
  </Manifest>
  <TypeDefinitions>
    <ModuleTypes>
      <DataSourceModuleType ID="code4ward.Sample.PowerShellMonitor.DataSource.CheckFileContent" Accessibility="Public" Batching="false">
        <Configuration>
          <xsd:element minOccurs="1" name="Interval" type="xsd:integer" />
          <xsd:element minOccurs="1" name="SyncTime" type="xsd:string" />
          <xsd:element minOccurs="1" name="File" type="xsd:string" />
          <xsd:element minOccurs="1" name="ErrorText" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="Interval" Selector="$Config/Interval$" ParameterType="int" />
          <OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string" />
          <OverrideableParameter ID="File" Selector="$Config/File$" ParameterType="string" />
          <OverrideableParameter ID="ErrorText" Selector="$Config/ErrorText$" ParameterType="string" />
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
        </OverrideableParameters>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <DataSource ID="Scheduler" TypeID="System!System.Scheduler">
                <Scheduler>
                  <SimpleReccuringSchedule>
                    <Interval>$Config/Interval$</Interval>
                    <SyncTime>$Config/SyncTime$</SyncTime>
                  </SimpleReccuringSchedule>
                  <ExcludeDates />
                </Scheduler>
              </DataSource>
              <ProbeAction ID="Probe" TypeID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent">
                <File>$Config/File$</File>
                <ErrorText>$Config/ErrorText$</ErrorText>
                <Debug>$Config/Debug$</Debug>
                <ExecutedAsTask>false</ExecutedAsTask>
              </ProbeAction>
            </MemberModules>
            <Composition>
              <Node ID="Probe">
                <Node ID="Scheduler" />
              </Node>
            </Composition>
          </Composite>
        </ModuleImplementation>
        <OutputType>System!System.PropertyBagData</OutputType>
      </DataSourceModuleType>
      <ProbeActionModuleType ID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent" Accessibility="Public" Batching="false" PassThrough="false">
        <Configuration>
          <xsd:element minOccurs="1" name="File" type="xsd:string" />
          <xsd:element minOccurs="1" name="ErrorText" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
          <xsd:element minOccurs="1" name="ExecutedAsTask" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="File" Selector="$Config/File$" ParameterType="string" />
          <OverrideableParameter ID="ErrorText" Selector="$Config/ErrorText$" ParameterType="string" />
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
          <OverrideableParameter ID="ExecutedAsTask" Selector="$Config/ExecutedAsTask$" ParameterType="bool" />
        </OverrideableParameters>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <ProbeAction ID="Probe" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
                <ScriptName>code4ward.Sample.PowerShellMonitor.ps1</ScriptName>
                <ScriptBody><![CDATA[param([string]$file, [string]$errorText, $debug, $executedAsTask)

if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
if ($executedAsTask -ne "true"){$executedAsTask = [bool]$false}else{$executedAsTask = [bool]$true}

$Script:API             = new-object -comObject "MOM.ScriptAPI"
$Script:Bag 			= $Script:API.CreatePropertyBag()
$Script:LOG_ERROR       = 1
$Script:LOG_WARNING     = 2
$Script:LOG_INFORMATION = 4
$Script:ScriptName      = "code4ward.Sample.PowerShellMonitor.ps1"
$Script:Arguments       = "Received Arguments:`rFile = $file`rErrorText = $errorText`rDebug = $debug`rExecutedAsTask = $executedAsTask"

function Write-DebugInfo([string] $msg)
{
    if ($debug) 
    {
    	$Script:API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
    }
}
function Write-ErrorInfo([string] $msg)
{
    $Script:API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
}

Write-DebugInfo "Script started..."

if ($file.Trim().Length -gt 0)
{
	$FileExists = (Test-Path $file)
}
else
{
	Write-ErrorInfo "No file was specified."
	return
}
		
if (-not $FileExists) 
{
	Write-ErrorInfo = "File '$file' not found."
	return
}
	
$File = Get-Item $file
[string]$FileContent = @()
foreach ($Line in Get-Content $File)
{
	$FileContent += "`r$Line"
}
	

if ($errorText.Trim().Length -gt 0)
{
	if ($FileContent.Contains($errorText))
	{
	   $Script:Bag.AddValue("Status","ERROR")
	   $Script:Bag.AddValue("MessageText","Error text '$errorText' found in file '$file'.")
	}
	else
	{
	   $Script:Bag.AddValue("Status","OK")
	   $Script:Bag.AddValue("MessageText","Error text '$errorText' not found in file '$file'.")
    }
    if ($executedAsTask)
    {
        $Script:Bag.AddValue("AdditionalStuff","goes here")
    }
}
else
{
    Write-ErrorInfo "No error text specified"
}

#$Script:API.Return($Bag)
$Script:Bag

Write-DebugInfo "Script ended..."
    ]]></ScriptBody>
                <Parameters>
                  <Parameter>
                    <Name>File</Name>
                    <Value>$Config/File$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>ErrorText</Name>
                    <Value>$Config/ErrorText$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>Debug</Name>
                    <Value>$Config/Debug$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>ExecutedAsTask</Name>
                    <Value>$Config/ExecutedAsTask$</Value>
                  </Parameter>
                </Parameters>
                <TimeoutSeconds>300</TimeoutSeconds>
              </ProbeAction>
            </MemberModules>
            <Composition>
              <Node ID="Probe" />
            </Composition>
          </Composite>
        </ModuleImplementation>
        <OutputType>System!System.PropertyBagData</OutputType>
        <InputType>System!System.BaseData</InputType>
      </ProbeActionModuleType>
      <ProbeActionModuleType ID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContentAsTask" Accessibility="Internal" Batching="false" PassThrough="false">
        <Configuration>
          <xsd:element minOccurs="1" name="File" type="xsd:string" />
          <xsd:element minOccurs="1" name="ErrorText" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
        </OverrideableParameters>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <ProbeAction ID="Probe" TypeID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent">
                <File>$Config/File$</File>
                <ErrorText>$Config/ErrorText$</ErrorText>
                <Debug>$Config/Debug$</Debug>
                <ExecutedAsTask>true</ExecutedAsTask>
              </ProbeAction>
            </MemberModules>
            <Composition>
              <Node ID="Probe" />
            </Composition>
          </Composite>
        </ModuleImplementation>
        <OutputType>System!System.PropertyBagData</OutputType>
        <InputType>System!System.BaseData</InputType>
      </ProbeActionModuleType>
    </ModuleTypes>
    <MonitorTypes>
      <UnitMonitorType ID="code4ward.Sample.PowerShellMonitor.MonitorType.CheckFileContent" Accessibility="Public">
        <MonitorTypeStates>
          <MonitorTypeState ID="Healthy" NoDetection="false" />
          <MonitorTypeState ID="Unhealthy" NoDetection="false" />
        </MonitorTypeStates>
        <Configuration>
          <xsd:element minOccurs="1" name="Interval" type="xsd:integer" />
          <xsd:element minOccurs="1" name="SyncTime" type="xsd:string" />
          <xsd:element minOccurs="1" name="File" type="xsd:string" />
          <xsd:element minOccurs="1" name="ErrorText" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="Interval" Selector="$Config/Interval$" ParameterType="int" />
          <OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string" />
          <OverrideableParameter ID="ErrorText" Selector="$Config/ErrorText$" ParameterType="string" />
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
        </OverrideableParameters>
        <MonitorImplementation>
          <MemberModules>
            <DataSource ID="DS" TypeID="code4ward.Sample.PowerShellMonitor.DataSource.CheckFileContent">
              <Interval>$Config/Interval$</Interval>
              <SyncTime>$Config/SyncTime$</SyncTime>
              <File>$Config/File$</File>
              <ErrorText>$Config/ErrorText$</ErrorText>
              <Debug>$Config/Debug$</Debug>
            </DataSource>
            <ProbeAction ID="Script" TypeID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent">
              <File>$Config/File$</File>
              <ErrorText>$Config/ErrorText$</ErrorText>
              <Debug>$Config/Debug$</Debug>
              <ExecutedAsTask>false</ExecutedAsTask>
            </ProbeAction>
            <ProbeAction ID="PassThrough" TypeID="System!System.PassThroughProbe" />
            <ConditionDetection ID="HealthyExpression" TypeID="System!System.ExpressionFilter">
              <Expression>
                <SimpleExpression>
                  <ValueExpression>
                    <XPathQuery Type="String">Property[@Name='Status']</XPathQuery>
                  </ValueExpression>
                  <Operator>Equal</Operator>
                  <ValueExpression>
                    <Value Type="String">OK</Value>
                  </ValueExpression>
                </SimpleExpression>
              </Expression>
            </ConditionDetection>
            <ConditionDetection ID="UnhealthyExpression" TypeID="System!System.ExpressionFilter">
              <Expression>
                <SimpleExpression>
                  <ValueExpression>
                    <XPathQuery Type="String">Property[@Name='Status']</XPathQuery>
                  </ValueExpression>
                  <Operator>Equal</Operator>
                  <ValueExpression>
                    <Value Type="String">ERROR</Value>
                  </ValueExpression>
                </SimpleExpression>
              </Expression>
            </ConditionDetection>
          </MemberModules>
          <RegularDetections>
            <RegularDetection MonitorTypeStateID="Healthy">
              <Node ID="HealthyExpression">
                <Node ID="DS" />
              </Node>
            </RegularDetection>
            <RegularDetection MonitorTypeStateID="Unhealthy">
              <Node ID="UnhealthyExpression">
                <Node ID="DS" />
              </Node>
            </RegularDetection>
          </RegularDetections>
          <OnDemandDetections>
            <OnDemandDetection MonitorTypeStateID="Healthy">
              <Node ID="HealthyExpression">
                <Node ID="Script">
                  <Node ID="PassThrough" />
                </Node>
              </Node>
            </OnDemandDetection>
            <OnDemandDetection MonitorTypeStateID="Unhealthy">
              <Node ID="UnhealthyExpression">
                <Node ID="Script">
                  <Node ID="PassThrough" />
                </Node>
              </Node>
            </OnDemandDetection>
          </OnDemandDetections>
        </MonitorImplementation>
      </UnitMonitorType>
    </MonitorTypes>
  </TypeDefinitions>
  <Monitoring>
    <Monitors>
      <UnitMonitor ID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" Accessibility="Internal" Enabled="true" Target="SC!Microsoft.SystemCenter.RootManagementServer" ParentMonitorID="Health!System.Health.AvailabilityState" Remotable="true" Priority="Normal" TypeID="code4ward.Sample.PowerShellMonitor.MonitorType.CheckFileContent" ConfirmDelivery="true">
        <Category>AvailabilityHealth</Category>
        <AlertSettings AlertMessage="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck_AlertMessageResourceID">
          <AlertOnState>Error</AlertOnState>
          <AutoResolve>true</AutoResolve>
          <AlertPriority>High</AlertPriority>
          <AlertSeverity>Error</AlertSeverity>
          <AlertParameters>
            <AlertParameter1>$Data/Context/Property[@Name='MessageText']$</AlertParameter1>
          </AlertParameters>
        </AlertSettings>
        <OperationalStates>
          <OperationalState ID="UIGeneratedOpStateId518336f4650c487bab4b958ce789099e" MonitorTypeStateID="Healthy" HealthState="Success" />
          <OperationalState ID="UIGeneratedOpStateId221bfe349ca54da3a0465f69d827fa4b" MonitorTypeStateID="Unhealthy" HealthState="Error" />
        </OperationalStates>
        <Configuration>
          <Interval>300</Interval>
          <SyncTime />
          <File>c:\test.txt</File>
          <ErrorText>ERROR!</ErrorText>
          <Debug>false</Debug>
        </Configuration>
      </UnitMonitor>
    </Monitors>
    <Diagnostics>
      <Diagnostic ID="code4ward.Sample.PowerShellMonitor.Diagnostic.FileContentCheck" Accessibility="Internal" Enabled="false" Target="SC!Microsoft.SystemCenter.RootManagementServer" Monitor="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" ExecuteOnState="Error" Remotable="true" Timeout="300">
        <Category>Operations</Category>
        <ProbeAction ID="Diagnostic" TypeID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContentAsTask">
          <File>c:\test.txt</File>
          <ErrorText>ERROR!</ErrorText>
          <Debug>false</Debug>
        </ProbeAction>
      </Diagnostic>
    </Diagnostics>
  </Monitoring>
  <Presentation>
    <StringResources>
      <StringResource ID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck_AlertMessageResourceID" />
    </StringResources>
  </Presentation>
  <LanguagePacks>
    <LanguagePack ID="DEA" IsDefault="false">
      <DisplayStrings>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor">
          <Name>code4ward Sample PowerShell Monitor</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" SubElementID="UIGeneratedOpStateId221bfe349ca54da3a0465f69d827fa4b">
          <Name>Unhealthy</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" SubElementID="UIGeneratedOpStateId518336f4650c487bab4b958ce789099e">
          <Name>Healthy</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck_AlertMessageResourceID">
          <Name>File content check detected a problem</Name>
          <Description>{0}</Description>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent">
          <Name>Check File Content</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContentAsTask">
          <Name>Check File Content as Task</Name>
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
    <LanguagePack ID="ENU" IsDefault="true">
      <DisplayStrings>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor">
          <Name>code4ward Sample PowerShell Monitor</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.DataSource.CheckFileContent">
          <Name>Check File Content Data Source</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Diagnostic.FileContentCheck">
          <Name>Execute File Content Check</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck">
          <Name>File Content Check Monitor</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" SubElementID="UIGeneratedOpStateId221bfe349ca54da3a0465f69d827fa4b">
          <Name>Unhealthy</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck" SubElementID="UIGeneratedOpStateId518336f4650c487bab4b958ce789099e">
          <Name>Healthy</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Monitor.FileContentCheck_AlertMessageResourceID">
          <Name>File content check detected a problem</Name>
          <Description>{0}</Description>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.MonitorType.CheckFileContent">
          <Name>Check File Content Monitor Type</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContent">
          <Name>Check File Content</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.PowerShellMonitor.Probe.CheckFileContentAsTask">
          <Name>Check File Content as Task</Name>
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
  </LanguagePacks>
</ManagementPack>