SCOM folders count monitoring


  • Hi,

    I'm trying to create a script monitor to alarm when the number of folders in a path is over 10.

    All the scripts i found are for files count and not folders. I need the count to only be for folders (there can be also files in the folder).

    Anyone have a script for that?

    Thank you

    Tuesday, June 26, 2018 1:36 PM

All replies

  • Well I don't know what script you have found, but a simple (Get-ChildItem -Directory c:\temp\myfolder | measure-object).Count should return you the child folders count under c\temp\myfolder

    (requires at least powershell 3.0)

    • Edited by CyrAz Tuesday, June 26, 2018 2:16 PM
    • Proposed as answer by Sameer Mhaisekar Tuesday, June 26, 2018 3:15 PM
    Tuesday, June 26, 2018 2:16 PM
  • Hi There,

    For this you need to create a powershell monitor as CyrAz suggested.

    First download Powershell community MP to create a 2 state script based monitor using powershell.

    Now create 2 state script monitor and paste this script into script section:-

    $ScomAPI        = New-Object -comObject "MOM.ScriptAPI"
    $PropertyBag    = $ScomAPI.CreatePropertyBag()
    $rawFileData    = (Get-ChildItem -Directory c:\temp | measure-object)
    $wellFormedData = ($rawFileData) -replace '\s{2,}',',' | ConvertFrom-Csv
    $wellFormedData | Where-Object {$_.Count -ge '10' }
    $messageText    = "Count of the file inside folder.`n`n"
    $messageText   += "Details: $($wellFormedData)"
    if($wellFormedData.Count -ge 10) {
    } else {

    Hope this helps :)

    Cheers, Gourav Please remember to mark the replies as answers if it helped.

    Wednesday, June 27, 2018 4:05 AM
  • There are a number of challenges with that script:

    1. Why is there a convertfrom-csv if all we are doing is counting folders?

    2. I'd try to include the health states in the script. Just pass back the value and do the evaluation later. This allows us to expose the threshold as an override. 

    I've done a walk through here which also includes:

    - a MatchCount parameter so that you include consecutive occurrences over threshold before triggering an alert

    - a scheduler filter to allow for only going unhealthy during business hourds

    - a diagnostic task to list the folders in the top level folder

    - a recovery task to delete the folders in the top level folder

    It does require a sound knowledge of Visual Studio Authoring but you can also just take the script from Part 1 and plug it into the Squared Up Community PowerShell MP. 

    Part 1 - The PowerShell -

    Part 2 - The Data Source and Probe -

    Part 3 - The Monitor Type -

    Part 4 - The Monitor -

    Part 5 - A diagnostic task to view all the folders in the top level folder -

    Part 6 - A recovery task to delete all the folders in the top level folder -



    Blog - Twitter -

    Saturday, June 30, 2018 5:46 PM