-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClass.And.Discovery.Script.PowerShell.FileExists.mpx
More file actions
227 lines (206 loc) · 10.4 KB
/
Class.And.Discovery.Script.PowerShell.FileExists.mpx
File metadata and controls
227 lines (206 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<ManagementPackFragment SchemaVersion="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
%%
Description:
A CLASS and DISCOVERY using a PowerShell Script to create class instances where a specific file exists on the filesystem
CompanyID - is a short abbreviation for your company with NO SPACES OR SPECIAL CHARACTERS ALLOWED
AppName - is a short name for your app with NO SPACES OR SPECIAL CHARACTERS ALLOWED
UniqueID - We need a Unique ID to reuse the fragment within the same MP. Use a shortened descriptive name with NO SPACES OR SPECIAL CHARACTERS ALLOWED
FilePath - This is the path to the file such as "C:\foo.txt"
In this example we added a a couple additional class properties to demo that capability
Version: 1.1
LastModified: 17-Jun-2021
%%
In this fragment you need to replace:
##CompanyID##
##AppName##
##UniqueID##
##FilePath##
This fragment depends on references:
RequiredReference: Alias="System", ID="System.Library"
RequiredReference: Alias="Windows", ID="Microsoft.Windows.Library"
@@Author=Kevin Holman@@
-->
<TypeDefinitions>
<EntityTypes>
<ClassTypes>
<ClassType ID="##CompanyID##.##AppName##.##UniqueID##.Class" Base="Windows!Microsoft.Windows.LocalApplication" Accessibility="Public" Abstract="false" Hosted="true" Singleton="false">
<!-- We choose Microsoft.Windows.LocalApplication as our generic base class -->
</ClassType>
</ClassTypes>
</EntityTypes>
<ModuleTypes>
<DataSourceModuleType ID="##CompanyID##.##AppName##.##UniqueID##.Class.Discovery.DS" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
<xsd:element name="SyncTime" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
<xsd:element name="TimeoutSeconds" type="xsd:integer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
<xsd:element name="DebugLogging" type="xsd:boolean" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
<xsd:element name="FilePath" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int" />
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string" />
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int" />
<OverrideableParameter ID="DebugLogging" Selector="$Config/DebugLogging$" ParameterType="bool" />
<OverrideableParameter ID="FilePath" Selector="$Config/FilePath$" ParameterType="string" />
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>##CompanyID##.##AppName##.##UniqueID##.Class.Discovery.DS.ps1</ScriptName>
<ScriptBody>
#=================================================================================
# Script to discover a SCOM class when a File Exists
#
# Author: Kevin Holman
# v1.0
#=================================================================================
param($SourceId,$ManagedEntityId,$ComputerName,$MGName,$DebugLogging,$FilePath)
# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $SourceId = '{00000000-0000-0000-0000-000000000000}'
# $ManagedEntityId = '{00000000-0000-0000-0000-000000000000}'
# $ComputerName = "computername.domain.com"
# $MGName = "MGNAME"
# $DebugLogging = "true"
# $FilePath = "C:\foo.txt"
#=================================================================================
# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging.
# ScriptName should be the same as the ID of the module that the script is contained in
$ScriptName = "##CompanyID##.##AppName##.##UniqueID##.Class.Discovery.DS.ps1"
$EventID = "4545"
#=================================================================================
# Starting Script section - All scripts get this
#=================================================================================
# Gather the start time of the script
$StartTime = Get-Date
#Set variable to be used in logging events
$whoami = whoami
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript is starting. `nFilePath: ($FilePath). `nRunning as ($whoami). `nManagement Group: ($MGName). `nDebugLogging: ($DebugLogging)")
#=================================================================================
# Discovery Script section - Discovery scripts get this
#=================================================================================
# Load SCOM Discovery module
$DiscoveryData = $momapi.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
#=================================================================================
# Begin MAIN script section
#=================================================================================
# Test to see if filepath exists
$FilePathExists = Test-Path $FilePath
# If filpath exists create instance of class
If ($FilePathExists -eq $true)
{
$instance = $DiscoveryData.CreateClassInstance("$MPElement[Name='##CompanyID##.##AppName##.##UniqueID##.Class']$")
$instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $ComputerName)
$instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $ComputerName)
$DiscoveryData.AddInstance($instance)
IF ($DebugLogging.ToUpper() -eq "TRUE")
{
# Log an event that objects were discovered
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`n Discovery script is returning objects for ($ComputerName) because ($FilePath) exists.")
}
}
Else
{
IF ($DebugLogging.ToUpper() -eq "TRUE")
{
# Log an event for no objects discovered
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`n Discovery script returned no discovered objects")
}
}
# Return Discovery Items Normally
$DiscoveryData
# Return Discovery Bag to the command line for testing (does not work from ISE)
# $momapi.Return($DiscoveryData)
#=================================================================================
# End MAIN script section
# End of script section
#=================================================================================
#Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript Completed. `nScript Runtime: ($ScriptTime) seconds.")
#=================================================================================
# End of script
</ScriptBody>
<Parameters>
<Parameter>
<Name>SourceId</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>ManagedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>ComputerName</Name>
<Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
<Parameter>
<Name>MGName</Name>
<Value>$Target/ManagementGroup/Name$</Value>
</Parameter>
<Parameter>
<Name>DebugLogging</Name>
<Value>$Config/DebugLogging$</Value>
</Parameter>
<Parameter>
<Name>FilePath</Name>
<Value>$Config/FilePath$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS" />
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>
</ModuleTypes>
</TypeDefinitions>
<Monitoring>
<Discoveries>
<Discovery ID="##CompanyID##.##AppName##.##UniqueID##.Class.Discovery" Target="Windows!Microsoft.Windows.Server.OperatingSystem" Enabled="true" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<!-- We choose Microsoft.Windows.Server.OperatingSystem as the preferred target class to ensure this will run on all Windows Servers, but will not create duplicates on clusters -->
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="##CompanyID##.##AppName##.##UniqueID##.Class">
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="##CompanyID##.##AppName##.##UniqueID##.Class.Discovery.DS">
<IntervalSeconds>86333</IntervalSeconds>
<SyncTime></SyncTime>
<TimeoutSeconds>120</TimeoutSeconds>
<DebugLogging>false</DebugLogging>
<FilePath>##FilePath##</FilePath>
</DataSource>
</Discovery>
</Discoveries>
</Monitoring>
<LanguagePacks>
<LanguagePack ID="ENU" IsDefault="true">
<DisplayStrings>
<!-- Classes -->
<DisplayString ElementID="##CompanyID##.##AppName##.##UniqueID##.Class">
<Name>##CompanyID## ##AppName## ##UniqueID## Class</Name>
</DisplayString>
<!-- Discoveries -->
<DisplayString ElementID="##CompanyID##.##AppName##.##UniqueID##.Class.Discovery">
<Name>##CompanyID## ##AppName## ##UniqueID## Class Discovery</Name>
</DisplayString>
</DisplayStrings>
</LanguagePack>
</LanguagePacks>
</ManagementPackFragment>