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.

PowerShell to watch a folder and return file counts

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

Parents
  • It's throwing the error because the query for files older than one hour returned no results. This means that $lasthour is null (or empty), causing the error to come up. It looks like the same applies to $allFiles as well.

    There could be a couple of causes for this. The first is a permissions issue.  I noticed in the script that it is attempting to access the remote fie server using the special admin share F$.  Do you specify the check to run as a specific account in the check properties? Are you able to run that powershell script as that same account from the server running Solarwinds?

Reply
  • It's throwing the error because the query for files older than one hour returned no results. This means that $lasthour is null (or empty), causing the error to come up. It looks like the same applies to $allFiles as well.

    There could be a couple of causes for this. The first is a permissions issue.  I noticed in the script that it is attempting to access the remote fie server using the special admin share F$.  Do you specify the check to run as a specific account in the check properties? Are you able to run that powershell script as that same account from the server running Solarwinds?

Children