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 #########

  • So at 12:01 AM, this might return 0 files - just so you are aware.

    $Today = Get-Date | Select-Object -Property Date | Get-Date
    $Path = "\\Path\To\Check"
    $Files = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Today }
    Write-Host "Message: Found $( $Files.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic: $( $Files.Count )"

    Message: Found 1836 files in \\Path\To\Check created on 06/01/2022
    Statistic: 1836

  • Here is a way to show both ways (since midnight today and within the last 24 hours)

    $Path = "\\Path\To\Check"
    
    # 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 }
    
    # Things since this time yesterday
    $Last24Hours = ( Get-Date ).AddDays(-1)
    $FilesLast24Hours = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Last24Hours }
    
    # 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 )"
    
    # Results for 'Last 24 hours'
    Write-Host "Message.Last24Hours: Found $( $FilesLast24Hours.Count ) files in $( $Path ) created on or after $( $Last24Hours.ToShortDateString() ) $( $Last24Hours.ToShortTimeString() )"
    Write-Host "Statistic.Last24Hours: $( $FilesLast24Hours.Count )"

  • Thanks 
    Ill test and get back to you Slight smile

  • its showing wrong 

    Found 0 files in PATH created on 06/01/2022

    2 file available there

  • I wonder if it's a permission issue.  Can we try this script instead?

    $Today = Get-Date | Select-Object -Property Date | Get-Date
    $Path = "\\Path\To\Check"
    
    $AllFiles = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue
    $Files = $AllFiles | Where-Object { $_.LastWriteTime -ge $Today }
    
    Write-Host "Message.All: Found $( AllFiles.Count) files in $( $Path )"
    Write-Host "Statistic.All: $( AllFiles.Count)"
    
    Write-Host "Message.Today: Found $( $Files.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"
    Write-Host "Statistic.Today: $( $Files.Count )"

    If both results show 0, then your account can't read the files.

  • Hi still some error in script not getting right result, file is there still showing 0 count, count should be one

  • Did you try the follow-up script to see if it's a permission issue?

  • Yes, its working on another server.

    but in last 24 hrs. script has some issue, as its count last and latest file 
    suppose one files created on 16th June by 4 AM and one files created on 17th June by 4 AM, so it count 2 file, so i have converted into minutes, for workaround, can you share script who count those file only created on same date. 

Reply
  • Yes, its working on another server.

    but in last 24 hrs. script has some issue, as its count last and latest file 
    suppose one files created on 16th June by 4 AM and one files created on 17th June by 4 AM, so it count 2 file, so i have converted into minutes, for workaround, can you share script who count those file only created on same date. 

Children
  • Just change the LastWriteTime in the first script to CreationTime.

  • Hi,

    we need script to check current date count only

    see below script showing 0 count, however we have 1 file for same date see on next screen short

  • 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"
    }