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