1: <ScriptName>DiscoverFileContentChecks.ps1</ScriptName>
2: <ScriptBody><![CDATA[
3: param($mapElement, $targetID, $computerName, $debug)
4:
5: #Error-Handler
6: trap
7: {
8: continue;
9: }
10:
11: function Main
12: {
13: trap{continue}
14: #Set Error and Debug Strings
15: $Error.Clear()
16: if ($debug -ne "true"){$debug = [bool]$false}else{$debug = [bool]$true}
17: $Script:Err = @()
18: $Script:API = new-object -comObject "MOM.ScriptAPI"
19: $Script:LOG_ERROR = 1
20: $Script:LOG_WARNING = 2
21: $Script:LOG_INFORMATION = 4
22: $Script:ScriptName = "code4ward.Sample.PowerShellDiscovery.ps1"
23: $Script:Arguments = "Received Arguments: `r MPElement = $mapElement, `rTargetID = $targetID, `rComputerName = $computerName, `rDebug = $debug"
24:
25: #Set the Discovery Object and the Check-Hive
26: $Script:Discovery = $Script:API.CreateDiscoveryData(0, $mapElement, $targetID)
27: $Script:CheckHive = "HKLM:\SOFTWARE\code4ward\SCOM"
28:
29: # Discover File-Content-Checks all Checks
30: Discover-FileContentChecks("$Script:CheckHive\FileContentChecks")
31:
32: #Return Discovery Data
33: Write-DebugInfo("Returning Discovery Data")
34: $Script:Discovery
35: }
36:
37: function Discover-FileContentChecks([string]$fileContentChecksPath)
38: {
39: Write-DebugInfo("Running Discovery for the FileContentChecks")
40:
41: if (-not (Test-Path $fileContentChecksPath))
42: {
43: Write-DebugInfo("The path specified for the FileContentChecksPath [$fileContentChecksPath] can't be found on this computer, skipping this Discovery.")
44: }
45: else
46: {
47: $FileContentChecks = @(Get-ChildItem $fileContentChecksPath)
48:
49: foreach ($FileContentCheckPath in $FileContentChecks)
50: {
51: #Get Values and add them to Variables
52: #Inherited Properties
53: $PropertyName = $FileContentCheckPath.Name.Replace($fileContentChecksPath.Replace("HKLM:", "HKEY_LOCAL_MACHINE") + "\", "")
54: $PropertyID = "FileContentCheck." + $PropertyName.Replace(" ", "")
55:
56: #Class-Properties
57: $PropertyFile = $FileContentCheckPath.GetValue("File")
58: $PropertyErrorText = $FileContentCheckPath.GetValue("ErrorText")
59:
60: write-debuginfo("Received Name: $PropertyName ID: $PropertyID File: $PropertyFile ErrorText: $PropertyErrorText")
61:
62: $Instance = $Script:Discovery.CreateClassInstance("$MPElement[Name='code4ward.Sample.PowerShellDiscovery.Classes.CheckFileContent']$")
63: $Instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $PropertyName)
64: $Instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerName)
65: $Instance.AddProperty("$MPElement[Name='code4ward.Sample.PowerShellDiscovery.Classes.CheckFileContent']/ID$", $PropertyID)
66: $Instance.AddProperty("$MPElement[Name='code4ward.Sample.PowerShellDiscovery.Classes.CheckFileContent']/File$", $PropertyFile)
67: $Instance.AddProperty("$MPElement[Name='code4ward.Sample.PowerShellDiscovery.Classes.CheckFileContent']/ErrorText$", $PropertyErrorText)
68:
69: #Return Instance to Discovery Data
70: Write-DebugInfo("Adding discovered Instance to Discovery")
71: if(-not $Error.Count -gt 0){$Script:Discovery.AddInstance($Instance)}else{Write-ErrorInfo("Discover-FileContentChecks-$TargetID")}
72: }
73: }
74: }
75:
76: function Write-DebugInfo
77: {
78: param ([string] $msg)
79: if ($debug -eq $true)
80: {
81: $API.LogScriptEvent("$ScriptName",100,$Script:LOG_INFORMATION,"`r$Arguments`r`r$msg")
82: }
83: }
84:
85: function Write-WarningInfo
86: {
87: param ([string] $msg)
88: $API.LogScriptEvent("$ScriptName",200,$Script:LOG_WARNING,"`r$Arguments`r`r$msg")
89: }
90:
91: function Write-ErrorInfo
92: {
93: param ([string] $msg)
94: $API.LogScriptEvent("$ScriptName",500,$Script:LOG_ERROR,"`r$Arguments`r`r$msg")
95: }
96:
97: #Call the MainFunction
98: Main
99: ]]></ScriptBody>
100: <Parameters>
101: <Parameter>
102: <Name>MapElement</Name>
103: <Value>$Config/MapElement$</Value>
104: </Parameter>
105: <Parameter>
106: <Name>TargetID</Name>
107: <Value>$Config/TargetID$</Value>
108: </Parameter>
109: <Parameter>
110: <Name>ComputerName</Name>
111: <Value>$Config/ComputerName$</Value>
112: </Parameter>
113: <Parameter>
114: <Name>Debug</Name>
115: <Value>$Config/Debug$</Value>
116: </Parameter>
117: </Parameters>
118: <TimeoutSeconds>600</TimeoutSeconds>