none
PropertyBag and custom alerting

    Question

  • HI there,

    I've found recently the great MP by Square up for Powershell scripts and started to use it successfully.

    Now I'm running a simple power shell to check certain folders for files and if there are files of specific type an alarm is being triggered.
    Now I would like to output the directories in which files are stored to SCOM alert description but I'm unable to and I think I have some fundamental errors in the way how I use the property bag.
    So let me give an example of the script:


    $ScomAPI = New-Object -comObject "MOM.ScriptAPI"
    $PropertyBag = $ScomAPI.CreatePropertyBag()
    $Count = ( Get-ChildItem 'E:\CS*\Error\*.csv' | Measure-Object ).Count
    $DirPath = ( dir 'E:\CS*\Error\*.csv' -Recurse | Select Directory -Unique )
    
    If ($Count -lt 1)
    {
       $PropertyBag.AddValue("State","Empty") 
    }
    Else
    {
      $PropertyBag.AddValue("State","Error")
    }
    $PropertyBag.AddValue("Directory",[string]$DirPath)
    
    $PropertyBag
                

    In this example the alarms triggers fine, it's base on the "State" PropertyBag.
    However if I try to use the "Directory" PropertyBag for alerting it doesn't work.
    In my alerting tab of the Monitor I have configured the following:

    Folder:
    $Data/Context/Property[@Name='Directory']$

    And this doesn't work :(
    Any ideas or advice would be much appreciated.

    Also have a look what it says in the PowerShell Example script provided by the MP:

    # Any Arguments specified will be sent to the script as a single string.
    # If you need to send multiple values, delimit them with a space, semicolon or other separator and then use split.
    param([string]$Arguments)
    
    $ScomAPI = New-Object -comObject "MOM.ScriptAPI"
    $PropertyBag = $ScomAPI.CreatePropertyBag()
    
    # Example of use below, in this case return the length of the string passed in and we'll set health state based on that.
    # Since the health state comparison is string based in this template we'll need to create a state value and return it.
    # Ensure you return a unique value per health state (e.g. a service status), or a unique combination of values.
    
    $PropertyBag.AddValue("MessageText",$Arguments)
    $PropertyBag.AddValue("Length",$Arguments.length)   
    
    if($Arguments.length -gt 10) {
      $PropertyBag.AddValue("State","OverThreshold")
    }
    else
    {
      $PropertyBag.AddValue("State","UnderThreshold")
    }
                 
    # Send output to SCOM
    $PropertyBag



    • Edited by Sakrayaami Friday, February 1, 2019 6:14 PM
    Friday, February 1, 2019 6:11 PM

Answers

  • Apologies for not replying sooner.
    So just to understand this PowerShell script part you have is this being used in a discovery or are you using a PowerShell Script based monitor which uses a custom data source? From what i can see it looks to be a PowerShell Script based monitor

    For the string resource you need to add the switches for it so underneath monitoring you would put underneath the </Monitoring> switch

    <Presentation>
     <StringResource ID="Custom.Alert.Description" />
     </Presentation>

    Then in the displaystrings declare it from here and the description should fill it in

    <DisplayString ElementID="Custom.Alert.Description"
     <Name>Custom Alert Description</Name>
     <Description>$Data/Context/Property[@Name='Directory']$</Description>
    </DisplayString>

    What you could also do is add the Alert Parameters in your MP so that you can refer to the Data Variable alot easier so you would put

    <AlertParameters>
     <AlertParameter1>$Data/Context/Property[@Name='Directory']$</AlertParameter1>
    </AlertParameters>

    Then in the Displaystrings part above where it says description you can put {0} to refer to it
    Give that a try


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    • Marked as answer by Sakrayaami Wednesday, February 13, 2019 11:05 AM
    Thursday, February 7, 2019 11:06 AM

All replies

  • In order for you get those into your Alert Description you would need to ensure that not only the directory is captured in your PowerShell property bag but you will also need to specify a string resource in your MP.
    Then in the Displaystrings when you reference the string resource ID you should be able to enter in your parameter/variable into the Description part of it

    Try removing the [string] switch as that should work, also I would move that line above where it goes into an IF statement


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Monday, February 4, 2019 10:03 AM
  • Thank you for the answer .

    I have tried to remove the [string] but the situation stays the same.
    I was hoping to find a way and modify the result instead of specifying a string resource in my MP.
    This was based due to the fact that if I record a number in the property bag, the alerting works as expected and it outputs the number in the Alert description.
    Would it be possible to provide a bit more explanation about hot to specify the string resource in the MP?

    Thank you,
    Monday, February 4, 2019 10:28 AM
  • Apologies for not replying sooner.
    So just to understand this PowerShell script part you have is this being used in a discovery or are you using a PowerShell Script based monitor which uses a custom data source? From what i can see it looks to be a PowerShell Script based monitor

    For the string resource you need to add the switches for it so underneath monitoring you would put underneath the </Monitoring> switch

    <Presentation>
     <StringResource ID="Custom.Alert.Description" />
     </Presentation>

    Then in the displaystrings declare it from here and the description should fill it in

    <DisplayString ElementID="Custom.Alert.Description"
     <Name>Custom Alert Description</Name>
     <Description>$Data/Context/Property[@Name='Directory']$</Description>
    </DisplayString>

    What you could also do is add the Alert Parameters in your MP so that you can refer to the Data Variable alot easier so you would put

    <AlertParameters>
     <AlertParameter1>$Data/Context/Property[@Name='Directory']$</AlertParameter1>
    </AlertParameters>

    Then in the Displaystrings part above where it says description you can put {0} to refer to it
    Give that a try


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    • Marked as answer by Sakrayaami Wednesday, February 13, 2019 11:05 AM
    Thursday, February 7, 2019 11:06 AM
  • How did you get on with this?

    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Monday, February 11, 2019 10:21 AM
  • Your script is not right.

    In this place you'll get an array:

    $DirPath = ( dir 'E:\CS*\Error\*.csv' -Recurse | Select Directory -Unique )

    and after, you cast it to string:

    $PropertyBag.AddValue("Directory",[string]$DirPath)

    First of all, if you want to create a monitor then you should discover your folders first. Secondly, best practice to compare states in ExpressionFilter module but not in a script.


    Vladimir Zelenov | http://systemcenter4all.wordpress.com



    • Edited by Vladimir Zelenov Monday, February 11, 2019 1:33 PM
    • Proposed as answer by GouravIN Tuesday, February 12, 2019 5:06 PM
    Monday, February 11, 2019 1:30 PM