Path Folder count monitoring

Hi All,

I am looking for a PowerShell script that can check path and count how many folder available in that path

example i have a path : \\XXXXSSQLP01\Team\work 
if we have 2 or more folders available in that path alert will triggred

can any one help in this

thanks

Sumit Goel

Parents
  • This is more than an alert.  You need to first build a monitor in SAM to check for the folder count.  There's an out of the box one that does it using PowerShell, but this is basically what it does.

    $Folders = Get-ChildItem -path \\ServerName\SharedFolder\ChildPath -Recurse -Folder
    if ( $Folders.Count -ge 2 ) {
        Write-Host "Message: We found $( $Folders.Count ) Folders - that's bad"
        Write-Host "Statistic: $( $Folders.Count )"
    }
    else {
        Write-Host "Message: We found $( $Folders.Count ) Folders - that's good"
        Write-Host "Statistic: $( $Folders.Count )"
    }

    Then you just need to alter the definition of the component so that if the statistic is 2 or more, it returns a critical status.

    Then the alert is easy: Alert when a component name is "Whatever you called this component" and the status is "Critical" fire an alert.

  • sorry but i am looking for file count monitoring for latest date

    i have prepared some thing but not able to print latest count with date

    get-childitem '\\NJIOILMSSQLU01\ilmsdata\VendorExport\NachaExport' |
    select @{n='Date';e={$_.LastWriteTime.ToShortDateString()}} |
    Group-Object Date |
    Format-Table Name, Count

    IF ($Count -gt 2)
    {
    $Message = 'there are wrong/duplicate files generated'
    Write-Host 'Statistic.Status: '2
    Write-Host 'Message.Status: '$Message
    }
    ELSE
    {
    $Message = 'there are no wrong/duplicate files generated'
    Write-Host 'Statistic.Status: '1
    Write-Host 'Message.Status: '$Message
    }


    ######## End Script #########

  • This is probably a permission issue.  Does the SAM WMI account you are using have sufficient access to read that page?  On the previous page do you have the configuration setup to run in Local or Remote host mode?

  • yes, it has permission, as other script showing count 

    can you share scripts for check today or current date count?

  • You can find countless examples of file count scripts online (not just here) using PowerShell.  I can't teach years of PowerShell background via this single forum thread.  If you want physical books, I'm happy to provide recommendations that I used to learn the language.

    The thing you are looking at is either a -Filter parameter on the Get-ChildItem or tweaks to the Where-Object function.

  • so to check single/current date count do have any handy script?

  • That's already been provided.  The $Today variable stores the value of midnight today and counts anything that's been creates after that.

  • but below script not showing correct count

    # Things since midnight today
    $Today = Get-Date | Select-Object -Property Date | Get-Date
    $FilesToday = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Today }

    # Results for 'today'
    Write-Host "Message.Today: Found $( $FilesToday.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic.Today: $( $FilesToday.Count )"

  • When posting code, please use the Insert/Code option in the menu.  It makes it easier to read.

    # Clear any errors in the queue
    $Error.Clear()
    
    # Things since midnight today
    $Today = Get-Date | Select-Object -Property Date | Get-Date
    # This will look for all files (including in subfolders) in the path
    $Files = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue
    # This will filter to only items that have been written after midnight "today"
    $FilesToday = $AllFiles | Where-Object { $_.LastWriteTime -ge $Today }
    
    # Results for all files
    Write-Host "Message.All: Found $( $Files.Count ) files in $( $Path )"
    Write-Host "Statistic.All: $( $Files.Count )"
    
    # Results for 'today'
    Write-Host "Message.Today: Found $( $FilesToday.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic.Today: $( $FilesToday.Count )"
    
    # Check to ses if there are any errors
    if ( $Error ) {
        Write-Host "Message.Errors: Encountered ( $Error.Count) errors: $( $Error.Message -join '; ' )"
        Write-Host "Statistic.Errors: ( $Error.Count )"
    }
    else {
        Write-Host "Message.Errors: No errors detected"
        Write-Host "Statistic.Errors: 0"
    }

    I have a feeling that the -ErrorAction SilentlyContinue is suppressing some type of permission or access error.  This new script (above) shows any of those errors it encountered. That should help in troubleshooting the problem.

  • I used below script


    $Path = "\\servername\VendorExport\NachaExport"

    # Things since midnight today
    $Today = Get-Date | Select-Object -Property Date | Get-Date
    # This will look for all files (including in subfolders) in the path
    $Files = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue
    # This will filter to only items that have been written after midnight "today"
    $FilesToday = $AllFiles | Where-Object { $_.LastWriteTime -ge $Today }

    # Results for 'today'
    Write-Host "Message.Today: Found $( $FilesToday.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic.Today: $( $FilesToday.Count )"

    # Check to ses if there are any errors
    if ( $Error ) {
    Write-Host "Message.Errors: Encountered ( $Error.Count) errors: $( $Error.Message -join '; ' )"
    Write-Host "Statistic.Errors: ( $Error.Count )"
    }
    else {
    Write-Host "Message.Errors: No errors detected"
    Write-Host "Statistic.Errors: 0"
    }

  • I just tested this complete template for myself on a NAS server.  You can import the template and try to assign it.

    File and Folder Counts (PowerShell).apm-template

    Note: Scripts are not supported by SolarWinds Support and are only supported via THWACK.

  • Hi,

    I have tried that template earlier as well, but no luck 

    can you help me with below script, its showing date and count, but also giving some error 

    $count = @{}
    $size = @{}

    get-childitem 'path ' |
    foreach {
    $date = $_.lastwritetime.tostring('dd-MM-yyyy')
    $count[$date]++
    }
    $count.keys |
    sort|
    foreach {
    [PSCustomObject]@{
    Date = $_
    'Number of files' = $Count[$_]
    }
    } | format-table -AutoSize



Reply
  • Hi,

    I have tried that template earlier as well, but no luck 

    can you help me with below script, its showing date and count, but also giving some error 

    $count = @{}
    $size = @{}

    get-childitem 'path ' |
    foreach {
    $date = $_.lastwritetime.tostring('dd-MM-yyyy')
    $count[$date]++
    }
    $count.keys |
    sort|
    foreach {
    [PSCustomObject]@{
    Date = $_
    'Number of files' = $Count[$_]
    }
    } | format-table -AutoSize



Children