How can I add SCOM 2012 Alert Context within a SCOM Alert When using a cusom powershell script in a two state monitor?

Hello, we have a two state monitor and I would like to channel the Alert Context to Alert Description.

The powershell script being used to find the stopped services is pasted below:

$API = new-object -comObject "MOM.ScriptAPI" 

$bag = $api.CreatePropertyBag()

$ServiceStatus = Get-WmiObject -class win32_service |where {$_.startmode -eq'Auto'-and $_.State -eq 'Stopped' -and

$_.name -ne 'wuauserv'}

if (!$ServiceStatus) #if there are no automatic services that are stopped, we are healthy.

{

write-host "good"

$bag.addvalue("State","good")

}

if ($servicestatus) #if there are any automatic services that are stopped, we are unhealthy.

{

write-host "bad"

$bag.addvalue("State","bad")

foreach ($service in $servicestatus) #enumerate the stopped services

{

$bag.addValue("$($Service.Name):","$($Service.state)")

}

}

$bag

$API.Return($BAG)

The results of the services that are stopped are being captured in the Alert Context. How can I pipe the Alert Context to Alert Description to obtain a meaningful alert?

Alert Context data:
Property Name                              Property Value

State                                                   bad

clr_optimization_v4.0.30319_32: Stopped

clr_optimization_v4.0.30319_64: Stopped

How can I pipe to Alert Description in SCOM 2012?

Thanks,

Kapi

May 12th, 2014 7:55pm

Hi,

maybe this help http://blogs.technet.com/b/jonathanalmquist/archive/2009/02/08/submitting-useful-property-bag-items-for-alert-description.aspx

Regards,

Ivan 

  • Proposed as answer by Mai AliMVP Tuesday, May 13, 2014 12:20 PM
Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 5:45am

Hi,

maybe this help http://blogs.technet.com/b/jonathanalmquist/archive/2009/02/08/submitting-useful-property-bag-items-for-alert-description.aspx

Regards,

Ivan 

  • Proposed as answer by Mai AliMVP Tuesday, May 13, 2014 12:20 PM
May 13th, 2014 5:45am

Agree with Ivan,

For Alert Description, you can refer below link

http://technet.microsoft.com/en-us/library/hh457540.aspx

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 12:19pm

Hi,

$Data/Context/Property[@Name='PBValue']$

May 13th, 2014 1:52pm

Thank you Alexis but what I am struggling with is that my PB in my PS script contains the $Service, and not a property bag with a value that could fit in ' '. What can we modify within the code to get the desired visibility within the Alert Description? We do see them in Alert Context but cannot pipe it.

$Data/Context/Property[@Name='PBValue']$ is what the links are hinting me towards but the PB value in my script is

$bag.addValue("$($Service.Name):","$($Service.state)")

Thanks,

Kapil Dham

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 2:26pm

OK,

$Data/Context/Property$

  • Marked as answer by Kapil Dham Wednesday, May 14, 2014 1:49 PM
May 13th, 2014 3:09pm

OK,

$Data/Context/Property$

  • Marked as answer by Kapil Dham Wednesday, May 14, 2014 1:49 PM
Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 3:09pm

Unfortunately, its returning only one word 'Bad' in the alert description if I paste $Data/Context/Property$ in the Alert description. Think its getting the property bag value when the service is detected from  the script excerpt below:

if ($servicestatus) #if there are any automatic services that are stopped, we are unhealthy.

{

write-host "bad"

$bag.addvalue("State","bad")

May 13th, 2014 3:26pm

you don't need write-host

you should create

$bag.addvalue("ServiceName1","StateOK")

$bag.addvalue("ServiceName2","StateStopped")

and in alert description you will see

ServiceName1   StateOK

ServiceName2   StateStopped

So try $bag.addValue($Service.Name,$Service.state)

and $Data/Context/Property$



  • Marked as answer by Kapil Dham Wednesday, May 14, 2014 1:49 PM
Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 3:33pm

you don't need write-host

you should create

$bag.addvalue("ServiceName1","StateOK")

$bag.addvalue("ServiceName2","StateStopped")

and in alert description you will see

ServiceName1   StateOK

ServiceName2   StateStopped

So try $bag.addValue($Service.Name,$Service.state)

and $Data/Context/Property$



  • Marked as answer by Kapil Dham Wednesday, May 14, 2014 1:49 PM
May 13th, 2014 3:33pm

So I finally got it working with help of systemcentercentral forum geniuses. The logic needed to be changed a bit within the script, in line with what Alexis wrote.

In order to add the logic to the Alert Description, I can get the services name using

$Data/Context/Property$

Here is the complete script, hope it helps anyone who is trying to accomplish something similar.

$API = new-object -comObject MOM.ScriptAPI
$bag = $api.CreatePropertyBag()

$StoppedServices = GWMI -class win32_service | where {$_.startmode -eq Auto-and $_.State -eq Stopped -and $_.name -ne wuauserv}

if ($StoppedServices) #if there are any Stopped Automatic services, we are unhealthy and need to list those services.
    {
    $serviceList=$null #establish a blank list of the stopped services
    foreach ($service in $StoppedServices) 
        {$servicelist= $servicelist + "`n" + $($service.displayname)} #add each stopped service and a line break to the list
    
    $bag.AddValue("StoppedServices","$serviceList") #add the list of services to the bag as one property
    $bag.addvalue(State,bad)
    }

else {$bag.addvalue(State,good)}  #if there are no Stopped Automatic services, we are healthy.

$API.Return($BAG)
"`n"
Free Windows Admin Tool Kit Click here and download it now
May 14th, 2014 1:48pm

This is exactly what I am looking for and I have successfully ran this in Powershell ISE, but now how do I actually implement it into SCOM 2012? Super newbie when using scom.
July 2nd, 2015 4:14pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics