This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

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

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

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

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

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



  • Click on the help link in the editor you showed above above.  There are very specific ways that PowerShell (Perl, Python, and other) templates have to be configured for output.