3 Replies Latest reply on Nov 20, 2019 9:43 PM by Jonathan Angliss

    PowerShell to watch a folder and return file counts

    stephend

      KMSigma provided this script as a starting point, but he's gotten busy with other things.  He suggested that I post it here.  Here's the script:

       

      $Folder = "F:\DuckCreek\Insights.Extract.Server\Errorlog\"

       

      $RemotePath = Join-Path -Path "\\${IP}\" -ChildPath $Folder.Replace(":", "$")

       

      $AllFiles = Get-ChildItem -Path $RemotePath | Where-Object { $_.Extension -in ( ".rq", ".rs" ) } | Sort-Object -Property CreationTime

       

      $LastHour = $AllFiles | Where-Object { $_.CreationTime -ge ( Get-Date ).AddHours(-1) }

       

      Write-Host "Message.LastHourCount: $( $LastHour.Count ) file(s) created in the last hour."

      Write-Host "Statistic.LastHourCount: $( $LastHour.Count )"

       

      Write-Host "Message.LastHourOldest: $( $LastHour[0].FullName ) is the oldest file in the folder (from the last hour)."

      Write-Host "Statistic.LastHourOldest: $( $LastHour[0].CreationTime.ToString("yyyyMMdd") )"

       

      Write-Host "Message.LastHourOldest: $( $AllFiles[0].FullName ) is the oldest file in the folder (from forever)."

      Write-Host "Statistic.LastHourOldest: $( $AllFiles[0].CreationTime.ToString("yyyyMMdd") )"

       

       

      Here's the results that I need help with understanding what the error is:

       

      When I tried running your template and getting the errors below. Can the community review and help?  The overall script/template just needs to run once an hour.

       

       

      Output: ==============================================
      Message.LastHourCount: 0 file(s) created in the last hour.
      Statistic.LastHourCount: 0
      Message.LastHourOldest: is the oldest file in the folder (from the last hour).
      Statistic.LastHourOldest:
      Message.LastHourOldest: is the oldest file in the folder (from forever).
      Statistic.LastHourOldest:

      Errors: ==============================================
      Cannot index into a null array.
      At line:12 char:40
      + Write-Host "Message.LastHourOldest: $( $LastHour[0].FullName ) is the ...
      + ~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

      Cannot index into a null array.
      At line:13 char:42
      + ... .LastHourOldest: $( $LastHour[0].CreationTime.ToString("yyyyMMdd") )"
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

      Cannot index into a null array.
      At line:15 char:40
      + Write-Host "Message.LastHourOldest: $( $AllFiles[0].FullName ) is the ...
      + ~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

      Cannot index into a null array.
      At line:16 char:42
      + ... .LastHourOldest: $( $AllFiles[0].CreationTime.ToString("yyyyMMdd") )"
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray