<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</ID>
      <Version>1.0.0.0</Version>
    </Identity>
    <Name>code4ward.Sample</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>
      <ProbeActionModuleType ID="code4ward.Sample.Action.SetRegistryValue" Accessibility="Public" Batching="false" PassThrough="false">
        <Configuration>
          <xsd:element minOccurs="1" name="Path" type="xsd:string" />
          <xsd:element minOccurs="1" name="Name" type="xsd:string" />
          <xsd:element minOccurs="1" name="Value" type="xsd:string" />
          <xsd:element minOccurs="1" name="Type" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="Path" Selector="$Config/Path$" ParameterType="string" />
          <OverrideableParameter ID="Name" Selector="$Config/Name$" ParameterType="string" />
          <OverrideableParameter ID="Value" Selector="$Config/Value$" ParameterType="string" />
          <OverrideableParameter ID="Type" Selector="$Config/Type$" ParameterType="string" />
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
        </OverrideableParameters>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <ProbeAction ID="Script" TypeID="Windows!Microsoft.Windows.PowerShellProbe">
                <ScriptName>SetRegistryValue.ps1</ScriptName>
                <ScriptBody><![CDATA[
# -----------------------------------------------------------------------------
# Script Name:  BWIN.Script.Library.SetRegistryValue.ps1
# Description:  Checks for reg key and adds/sets value
# Parameter 1:  path  (e.g. "HKLM:\Software\BWIN\WhoAmI")
# Parameter 2:  name  (e.g. "Environment")
# Parameter 3:  value (e.g. "PROD;")
# Parameter 4:  type (e.g. "String", "Binary", "DWord", "MultString", "ExpandString")
# Parameter 5:  debug (e.g. "false", "true")
# -----------------------------------------------------------------------------
# Parameters
param([string]$path,[string]$name,[string]$value,[string]$type,[string]$debug)

#Error-Handler
trap 
{ 
	# this installs an error handler for the function
  	# exiting a trap with "continue" says
  	# to continue on the next line of the script
	continue;
}

#Set Error and Debug Strings
$Error.Clear()
if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
$Script:Err				= @()
$Script:API 			= new-object -comObject "MOM.ScriptAPI"
$Script:LOG_ERROR       = 1
$Script:LOG_WARNING     = 2
$Script:LOG_INFORMATION = 4
$Script:ScriptName		= "BWIN.Script.Library.SetRegistryValue.ps1"
$Script:Arguments		= "Received Arguments: `rPath='$path' `rName='$name' `rValue='$value' `rDebug='$debug'"

function Write-DebugInfo
{
	param ([string] $msg)
	if ($debug) 
	{
		$API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
	}
}
function Write-ErrorInfo
{
	param ([string] $msg)
	$API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
}
function Check-Error($fromFunction)
{
	#Feed the Error Variable
	$Errnum = 1
	
	foreach ($Fail in $Error)
	{
		$Script:Err +=  "`rError in function $fromFunction #$Errnum : $Fail"
		$Errnum = $Errnum + 1
	}

	#If there were any errors, write them to Eventlog
	if ($Error.Count -gt 0) 
	{
		Write-DebugInfo("There were $ErrNum errors. Sending them to the Eventlog.")
		Write-ErrorInfo($Script:Err)
		exit
	}
}

Write-DebugInfo("Script started.")

# check if registry key exists
if (Test-Path $path)
{
	Write-DebugInfo("Path $path found in registry.") 
}
else
{
	Write-DebugInfo("Path $path doesn't exist. Creating registry keys...")
	New-Item -Path $path -force | Out-Null
	Check-Error("Create registry key.")
}
New-ItemProperty -Path $path -name $name -value $value -Type $type -force | Out-Null
Check-Error("Create registry value.")
Write-DebugInfo("Registry value $value of type $type in $path $name created/updated.")
Write-Host "Registry value $value of type $type in $path $name created/updated."
Write-DebugInfo("Script ended.")]]></ScriptBody>
                <Parameters>
                  <Parameter>
                    <Name>Path</Name>
                    <Value>$Config/Path$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>Name</Name>
                    <Value>$Config/Name$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>Value</Name>
                    <Value>$Config/Value$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>Type</Name>
                    <Value>$Config/Type$</Value>
                  </Parameter>
                  <Parameter>
                    <Name>Debug</Name>
                    <Value>$Config/Debug$</Value>
                  </Parameter>
                </Parameters>
                <TimeoutSeconds>300</TimeoutSeconds>
              </ProbeAction>
            </MemberModules>
            <Composition>
              <Node ID="Script" />
            </Composition>
          </Composite>
        </ModuleImplementation>
        <OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
        <InputType>System!System.BaseData</InputType>
      </ProbeActionModuleType>
      <ProbeActionModuleType ID="code4ward.Sample.SetRegistryEnvironment" Accessibility="Public" Batching="false" PassThrough="false">
        <Configuration>
          <xsd:element minOccurs="1" name="Value" type="xsd:string" />
          <xsd:element minOccurs="1" name="Debug" type="xsd:boolean" />
        </Configuration>
        <OverrideableParameters>
          <OverrideableParameter ID="Environment" Selector="$Config/Value$" ParameterType="string" />
          <OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool" />
        </OverrideableParameters>
        <ModuleImplementation Isolation="Any">
          <Composite>
            <MemberModules>
              <ProbeAction ID="Probe" TypeID="code4ward.Sample.Action.SetRegistryValue">
                <Path>HKLM:\Software\code4ward</Path>
                <Name>Environment</Name>
                <Value>$Config/Value$</Value>
                <Type>String</Type>
                <Debug>$Config/Debug$</Debug>
              </ProbeAction>
            </MemberModules>
            <Composition>
              <Node ID="Probe" />
            </Composition>
          </Composite>
        </ModuleImplementation>
        <OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
        <InputType>System!System.BaseData</InputType>
      </ProbeActionModuleType>
    </ModuleTypes>
  </TypeDefinitions>
  <Monitoring>
    <Tasks>
      <Task ID="code4ward.Sample.Task.SetEnvironment" Accessibility="Internal" Enabled="true" Target="Windows!Microsoft.Windows.Computer" Timeout="300" Remotable="true">
        <Category>Custom</Category>
        <ProbeAction ID="SetEnv" TypeID="code4ward.Sample.SetRegistryEnvironment">
          <Value />
          <Debug>false</Debug>
        </ProbeAction>
      </Task>
    </Tasks>
  </Monitoring>
  <LanguagePacks>
    <LanguagePack ID="DEA" IsDefault="false">
      <DisplayStrings>
        <DisplayString ElementID="code4ward.Sample">
          <Name>code4ward Sample</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.Action.SetRegistryValue">
          <Name>Set Registry Value</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.SetRegistryEnvironment">
          <Name>Set Registry Environment</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.Task.SetEnvironment">
          <Name>Set Environment</Name>
          <Description />
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
    <LanguagePack ID="ENU" IsDefault="true">
      <DisplayStrings>
        <DisplayString ElementID="code4ward.Sample">
          <Name>code4ward Sample</Name>
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.Action.SetRegistryValue">
          <Name>Set Registry Value</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.SetRegistryEnvironment">
          <Name>Set Registry Environment</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="code4ward.Sample.Task.SetEnvironment">
          <Name>Set Environment</Name>
          <Description />
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
  </LanguagePacks>
</ManagementPack>