| Very fast publishing of some of my sample scripts and associated notes for both Powershell and VBScript unit monitors or script rules which you can use with SCOM (System Center Operations Manager aka OpsMgr) any version since including SCOM 2007. Definitely PowerShell monitor requires more effort because there is no PowerShell module data source available out-of-box not even with SCOM 2012 R2, but it is not usually a big task.
# Note: vbScript monitors do not need to be assigned Parameters
# using the GUI button. You can put the Target and MPElement
# references directly into the script body and SCOM Agent
# will replace them with their actual values in the script
# source code automaticaly just at the point when it extracts
# the script from management pack and before it starts
# its actual execution.
# Note: keep in mind that the $Target is not a powershell variable
# and that the HealthService will replace the whole $$ content
# in the script source code just before running the script.
# So you must type the $$ references as they are and do not
# construct them dynamically and do not duplicate/escape any
# characters inside the strings (such as quotes)
# It does not matter if you enclose the $$ references in double
# quotes or single quotes, because their content will change
# to a constant value once health service starts the script.
# Yet be carefull about the actual values containing special
# characters such as $ (dolars) or back ticks in case of double
# quotes and single quotes in case of single quote enclosure.
$ipAddress = '$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/IPAddress$'
$targetScomId = '$Target/Id$'
$mgmtGroup = '$Target/ManagementGroup/Name$'
$mgmtGroupId = '$Target/ManagementGroup/Id$'
$api = New-Object -Com MOM.ScriptAPI
$bag = $api.CreatePropertyBag()
$bag.AddValue('givenName', 'Ondrej')
$bag.AddValue('surname', 'Sevecek')
$bag.AddValue('country', 'CZ')
$bag.AddValue('age', 36)
$bag.AddValue('birth date', ([DateTime] '1979-09-29'))
$bag.AddValue('adult', $true)
# Note: This goes here to display the XML. When running in a module,
# use the following "return" syntax instead:
# return $bag
$api.Return($bag)
<# Results XML:
<DataItem type="System.PropertyBagData" time="2015-12-03T21:38:29.3494785+01:00" sourceHealthServiceId="5213671A-BC84-174D-A66A-76EA65EB1B22">
<Property Name="givenName" VariantType="8">Ondrej</Property>
<Property Name="surname" VariantType="8">Sevecek</Property>
<Property Name="country" VariantType="8">CZ</Property>
<Property Name="age" VariantType="3">36</Property>
<Property Name="birth date" VariantType="7">09/29/1979</Property>
<Property Name="adult" VariantType="11" Type="Boolean">true</Property>
</DataItem>
#>
# For the Unhealthy/Degraded/Healthy expressions in unit monitors use:
#
# Property/[@Name='country']
#
# For alerting from unit monitors use the following:
#
# $Data/Context/Property[@Name='givenName']
#
More complex one:
$api = New-Object -Com MOM.ScriptAPI
$bag = $api.CreatePropertyBag()
$bag.AddValue('givenName', 'Ondrej')
$bag.AddValue('surname', 'Sevecek')
$bag.AddValue('country', 'CZ')
$bag.AddValue('age', 36)
$bag.AddValueToCollection('parents', 'father', 'Stanislav')
$bag.AddValueToCollection('parents', 'mother', 'Jitka')
$bag.AddValueToCollection('children', 'child1', 'Jan')
$bag.AddValueToCollection('children', 'child2', 'Julie')
# Note: This goes here to display the XML. When running in a module,
# use the following "return" syntax instead:
# return $bag
$api.Return($bag)
<# Note:
<DataItem type="System.PropertyBagData" time="2015-12-03T21:50:25.8151602+01:00" sourceHealthServiceId="5213671A-BC84-174D-A66A-76EA65EB1B22">
<Property Name="givenName" VariantType="8">Ondrej</Property>
<Property Name="surname" VariantType="8">Sevecek</Property>
<Property Name="country" VariantType="8">CZ</Property>
<Property Name="age" VariantType="3">36</Property>
<Collection Name="parents">
<Property Name="father" VariantType="8">Stanislav</Property>
<Property Name="mother" VariantType="8">Jitka</Property>
</Collection>
<Collection Name="children">
<Property Name="child1" VariantType="8">Jan</Property>
<Property Name="child2" VariantType="8">Julie</Property>
</Collection>
</DataItem>
#>
Multiple items:
$api = New-Object -Com MOM.ScriptAPI
$bag = $api.CreatePropertyBag()
$bag.AddValue('givenName', 'Ondrej')
$bag.AddValue('surname', 'Sevecek')
$bag.AddValue('country', 'CZ')
$bag.AddValue('age', 36)
$api.AddItem($bag)
$bag = $api.CreatePropertyBag()
$bag.AddValue('givenName', 'Julie')
$bag.AddValue('surname', 'Seveckova')
$bag.AddValue('country', 'CZ')
$bag.AddValue('age', 2)
$api.AddItem($bag)
# Note: This goes here to display the XML. When running in a module,
# use the following "return" syntax with an array of $bag(s) instead:
# [Collections.ArrayList] $bags = @()
# [void] $bags.Add($bag)
# return $bags
$api.ReturnItems()
<# Results:
<Collection>
<DataItem type="System.PropertyBagData" time="2015-12-03T21:31:40.1009574+01:00" sourceHealthServiceId="5213671A-BC84-174D-A66A-76EA65EB1B22">
<Property Name="givenName" VariantType="8">Ondrej</Property>
<Property Name="surname" VariantType="8">Sevecek</Property>
<Property Name="country" VariantType="8">CZ</Property>
<Property Name="age" VariantType="3">36</Property>
</DataItem>
<DataItem type="System.PropertyBagData" time="2015-12-03T21:31:40.1165638+01:00" sourceHealthServiceId="5213671A-BC84-174D-A66A-76EA65EB1B22">
<Property Name="givenName" VariantType="8">Julie</Property>
<Property Name="surname" VariantType="8">Seveckova</Property>
<Property Name="country" VariantType="8">CZ</Property>
<Property Name="age" VariantType="3">2</Property>
</DataItem>
</Collection>
#>
Logging into the Operations Manager event log:
$api = New-Object -Com MOM.ScriptAPI
# Note: according to documentation use any number 0-20000,
# rather go for 10000-19999 according to my experience
$api.LogScriptEvent("SEVECEK SCOM script", 19999, 0, "Info event")
$api.LogScriptEvent("SEVECEK SCOM script", 19998, 1, "Error event")
$api.LogScriptEvent("SEVECEK SCOM script", 19997, 2, "Warning event")
And finally some VBScript example which must be enough to port from the previous Powershell samples:
set api = CreateObject("MOM.ScriptAPI")
set bag = api.CreatePropertyBag()
bag.AddValue "givenName", "Ondrej"
bag.AddValue "surname", "Sevecek"
bag.AddValue "country", "CZ"
bag.AddValue "age", 36
bag.AddValue "adult", True
bag.AddValue "birth date", CDate("1979-09-29")
api.AddItem bag
set bag = api.CreatePropertyBag()
bag.AddValue "givenName", "Julie"
bag.AddValue "surname", "Seveckova"
bag.AddValue "country", "CZ"
bag.AddValue "age", 2
bag.AddValue "adult", False
bag.AddValue "birth date", CDate("2014-04-04")
api.AddItem bag
api.LogScriptEvent "SEVECEK SCOM script", 19996, 0, "Info event from VBS"
api.ReturnItems
|