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.

Help needed with modifying PS script for folder age for SAM 6.2

I have the following requirements for a monitor

If there are any files that exist in this location that end in "_N", with a "Date Modified" > 1 day (or 24 hours) beyond the current date, send a notification to the High Jump Developers group with the following instructions

I have a PS script that i used to work which can monitor the amount of files based on the folder name, age and type of files. I need help with modification of the script to include the name of the files

Below is the script i want to modify which gives me the list of files in the folder based on the criteria. In this PS i want to add the naming requirement.

[string]$path = "\\AArcWiVPWb10021\inetpub\ftproot\CustEDI\Replenishment"

[string]$filter = "*.xml"

[int]$maxAgeMinutes = 20

# Count of overdue files

$cnt = (Get-ChildItem -Path $path -Filter $filter | Where-Object {$_.CreationTime -lt [DateTime]::Now.AddMinutes(-$maxAgeMinutes)}).Count

if ($cnt -eq $null) {

  $cnt = 0;

  }

Write-Host 'Statistic: ' $cnt

write-host 'Message: ' "\\AArcWiVPWb10021\inetpub\ftproot\CustEDI\Replenishment"

Exit 0;

if ($cnt -gt 0)

{

    exit (0)

} else {

    exit (1)

}

Any help is greatly appreciated

Parents
  • The -filter argument in Get-ChildItem matches any part of the file name.  So you can use -filter '*_N.xml' if that is the same extension, otherwise adjust your [string]$filter variable to match what you are looking for.

    Your script to modify is looking for files created over 20 minutes ago, you can either adjust the code to use hours, days, or adjust your [int]$maxAgeMinutes to be the number of minutes in a day (1440).  You'll also need to adjust the script to look at the LastWriteTime instead of CreationTime.

    Not sure I understand the "include the name of the files", so I'm assuming you mean you want the alert to include that information in it. So to string all the changes together you could make your script look like this:

    [string]$path = '\\AArxWiVPWb10021\inetpub\ftproot\CustEDI\Replenishment'

    [string]$filter = '*_N.xml'

    [int]$maxAgeMinutes = 1440

    $files = @(Get-ChildItem -Path $path -Filter $filter | Where-Object { $_.LastWriteTime -lt [DateTime]::Now.Addminutes(-$maxAgeMinutes) } )

    write-host $('Statistic: {0}' -f $files.count)

    write-host $('Message: Old Files {0}' -f $((files).Name -join ','))

    if ($files.Count -gt 0) {

    exit(1)

    } else {

    exit(0)

    }

    One issue might be the length of the files may make the Message line too long, and truncate stuff.  You can use the statistic value returned above to adjust the thresholds in the component monitor too.  Don't forget if you're accessing a network share, the server has to have access, or the account has to be impersonated.

    Edit: Missed a parenthesis and curly bracket (line #5 in code).

Reply
  • The -filter argument in Get-ChildItem matches any part of the file name.  So you can use -filter '*_N.xml' if that is the same extension, otherwise adjust your [string]$filter variable to match what you are looking for.

    Your script to modify is looking for files created over 20 minutes ago, you can either adjust the code to use hours, days, or adjust your [int]$maxAgeMinutes to be the number of minutes in a day (1440).  You'll also need to adjust the script to look at the LastWriteTime instead of CreationTime.

    Not sure I understand the "include the name of the files", so I'm assuming you mean you want the alert to include that information in it. So to string all the changes together you could make your script look like this:

    [string]$path = '\\AArxWiVPWb10021\inetpub\ftproot\CustEDI\Replenishment'

    [string]$filter = '*_N.xml'

    [int]$maxAgeMinutes = 1440

    $files = @(Get-ChildItem -Path $path -Filter $filter | Where-Object { $_.LastWriteTime -lt [DateTime]::Now.Addminutes(-$maxAgeMinutes) } )

    write-host $('Statistic: {0}' -f $files.count)

    write-host $('Message: Old Files {0}' -f $((files).Name -join ','))

    if ($files.Count -gt 0) {

    exit(1)

    } else {

    exit(0)

    }

    One issue might be the length of the files may make the Message line too long, and truncate stuff.  You can use the statistic value returned above to adjust the thresholds in the component monitor too.  Don't forget if you're accessing a network share, the server has to have access, or the account has to be impersonated.

    Edit: Missed a parenthesis and curly bracket (line #5 in code).

Children