none
SCOM folders count monitoring

    Question

  • 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.

    https://squaredup.com/content/management-packs/free-powershell-management-pack/

    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)"
     
    $PropertyBag.AddValue("MessageText",$messageText)
     
    if($wellFormedData.Count -ge 10) {
      $PropertyBag.AddValue("State","OverThreshold")
    } else {
      $PropertyBag.AddValue("State","UnderThreshold")
    }
                
    $PropertyBag
    

    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 - https://www.f1point2.com/the-script/

    Part 2 - The Data Source and Probe - https://www.f1point2.com/part-2-powershell-monitors-the-modules/

    Part 3 - The Monitor Type - https://www.f1point2.com/part-3-powershell-monitors-monitor-type/

    Part 4 - The Monitor - https://www.f1point2.com/part-4-the-monitor/

    Part 5 - A diagnostic task to view all the folders in the top level folder - https://www.f1point2.com/part-5-the-diagnostic/

    Part 6 - A recovery task to delete all the folders in the top level folder - https://www.f1point2.com/part-6-the-recovery/

    Cheers

    Graham


    Blog - http://www.f1point2.com/ Twitter - https://twitter.com/F1Point2_Graham

    Saturday, June 30, 2018 5:46 PM
    Moderator