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.

File Count Powershell

Hello,

I have some troubles with a custom file count script. The script should alert me when there is no new file in the directory for about 60 minutes.

Script:

$path = '\\server'

$stats = 0

$msg = ''

$days = 0

$hours = 3

$minutes = 0

$files = @(Get-ChildItem -Path $path)   

if ($files -ne $null) {

  $f_names = [System.String]::Join('|',$files)

  $msg = 'Message: ' + $f_names

  $stats = $files.Count

} else {

  $msg = 'Message: 0 files exceed defined age'

}

Write-Host $msg

Write-Host "Statistic: $stats"

The main problem is, the script is working fine and also counts the files.

pastedImage_2.png

But I´m not sure how to setup the static threshold to get the notifications. Is there a way to use a variable or something like this int the warning /less than field or something like that ?

pastedImage_3.png

Thank you very much!

  • I don't see a way that you script says "last X hours."  I would expect something more like:

    $HoursAgo = 1

    $MatchingFiles = Get-ChildItem -Path $Path -File | Where-Object { $_.CreationTime -gt ( Get-Date ).AddHours(-$HoursAgo) }

  • Hi KMSigma,

    Thanks for the replay.

    So you would do something like ?

    $path = '\\server\path\'

    $HoursAgo = 8

    $files = Get-ChildItem -Path $Path | Where-Object { $_.CreationTime -gt (Get-Date).AddHours(-$HoursAgo) }

    $f_names = [System.String]::Join('|',$files) 

    $msg = 'Last File: '+ $f_names

    Write-Host $msg

    Write-Host "File Count:" ( Get-ChildItem \\server\path\ ).Count;

    The output is okay, but when I test it I get an Output Result Error: Get Output Failed

    Thank you very much!

  • You need to at least three outputs from any PowerShell script. (Details here)

    1. Message (via Write-Host "Message: Your result is 11" )
    2. Statistic (via Write-Host "Statistic: 11" )
    3. An Exit Code (via exit $ExitCode (where $ExitCode is a number representing 0 = "Up", 1 = "Down", 2 = "Warning", 3 = "Critical", {else} = "Unknown") )

    Here's a simple implementation that I went with:

    $Path = "\\NOCFTP01v\WebRoot"

    $HoursAgo = 8

    # try..catch..finally block

    # try to do the stuff in the try block

    # only if you get an error, go to the catch block

    # regardless of what happens, run the finally block

    try

    {

        $Files = Get-ChildItem -Path $Path | Where-Object { $_.CreationTime -gt ( Get-Date ).AddHours( -$HoursAgo ) }

        $ExitCode = 0 # Up

        $Message = "Message: Last File(s): $( $Files.Name -join '|' )"

        $Statistic = "Statistic: $( $Files.Count )"

    }

    catch

    {

        $ExitCode = 1 # Down

        $Message = "Message: Error recieved: $( $Error[0].ErrorDetails )"

        $Statistic = "Statistic:"

    }

    finally

    {

        Write-Host $Message

        Write-Host $Statistic

        exit $ExitCode

    }

  • I'm trying to do the same thing. Were you able to get a script that worked?