How to monitor ZIP files older than 3 hours.

Need to monitor ZIP files that are older than 3 hours. I have the following code can someone let me know if will work ?

param([string]$path, [int]$minutes)
$path="\\SERVER\d$\InetPub_Test\KSFTPWebService\*.zip"
$stat=0
$msg=""
try
{    
    $LastWrite = $(Get-Date).AddMinutes(-$minutes)
    $FileArray = $(Get-ChildItem $path | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $LastWrite })
    if ($FileArray.Count -eq 0)
    {
        $msg = "No files found over $($minutes) 3 hours old"
    }
    else
    {
        $stat=$FileArray.Count
        $msg = "$($stat) files found older than $($minutes) hours: "
        foreach ($file in $FileArray)
        {
            $msg+=" " + $file + ","
        }
    }
}
catch
{
    $stat=1
    $msg=$_.Exception.Message
}

Write-Host "Statistic: $stat"
Write-Host "Message: $msg"

exit 0

Open in new window

And, this is how i run it
PS C:\Scripts> .\FileAgeMonitoringWildCard.ps1 "\\SERVER\d$\InetPub_Test\KSFTPWebService\*.zip", 180
Statistic: 0
Message: No files found over 0 3 hours old

Open in new window


Eventually, i need embed this script into solarwinds.

Thanks for your help,
namergSystems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
You already know how to run it.

Just change the second and third arguments to point to a folder with zip files of certain age. That should tell you if it works or not
0
Jose Gabriel Ortega CastroCEOCommented:
Copy this text and save it as: Check-ZipsOlderThanX.ps1
<#
.Synopsis
   Check 3-hour zip files in path
.DESCRIPTION
   Check 3-hour zip files in path
.EXAMPLE
   Check-ZipsOlderThanX.ps1 -path "D:\path"
.EXAMPLE
   Check-ZipsOlderThanX.ps1 -path "D:\path" -time 240
   (240 minutes => 4 hours
#>
[CmdletBinding()]
param(
    [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string[]]$path,
    [Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$true)]$time=180
)


$Global:ZipFiles=0
[System.Text.StringBuilder]$sb = [System.Text.StringBuilder]::new()

try{
    $AllZipFiles = Get-ChildItem -Recurse -Path $path -Include "*.zip" | where{ $_.CreationTime -lt $($(Get-Date).AddMinutes(-$time))}
    $Global:ZipFiles = $AllZipFiles.Count

    if($Global:ZipFiles -gt 0){
       
        foreach($file in $AllZipFiles){
            [void]$sb.AppendLine($file.FullName)
        }
    }
    else{
        Write-Host -ForegroundColor Cyan "No files found over $($time) minutes old"
        break;
    }
}
catch{
    $error=$_.Exception.Message
    Write-Error "There was an error, $error"
    break;
}

Write-Host -ForegroundColor Cyan "There were found $Global:ZipFiles files older than $time minutes on path"
Write-Host -ForegroundColor Gray $($sb.ToString())

Open in new window


And it's used like:

EXAMPLE1

   Check-ZipsOlderThanX.ps1 -path "D:\path"


EXAMPLE2

   Check-ZipsOlderThanX.ps1 -path "D:\path" -time 240


Also, use get-help .\check-zipsOlderThanX.ps1 -full
123.png
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
namergSystems AdministratorAuthor Commented:
Wow Jose Gabriel, let me test
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Jose Gabriel Ortega CastroCEOCommented:
Sure go ahead, let me know if you had any bug, I test it and I worked just fine, even have colors at the end haha
0
namergSystems AdministratorAuthor Commented:
jajaj, yeah you put some makeup on it lol
0
namergSystems AdministratorAuthor Commented:
Hmm got the following error:

Output: ==============================================
No files found over 180 minutes old 

Errors: ==============================================
Method invocation failed because [System.Text.StringBuilder] does not contain a method named 'new'. 
At line:20 char:1 
+ [System.Text.StringBuilder]$sb = [System.Text.StringBuilder]::new() 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException 
+ FullyQualifiedErrorId : MethodNotFound 

Get-ChildItem : Cannot find path 'C:\Windows\system32\"\SERVER\d$\InetPub_Test\KSFTPWebService' because it does not exist. 
At line:23 char:20 
+ $AllZipFiles = Get-ChildItem -Recurse -Path $path -Include "*.zip" | where{ ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...KSFTPWebService:String) [Get-ChildItem], ItemNotFoundException 
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand 

Open in new window

0
namergSystems AdministratorAuthor Commented:
see attachment
Screenshot_1.png
0
Jose Gabriel Ortega CastroCEOCommented:
Ok remember me the version of powershell by running $PSVersionTable

Change this line (20) [System.Text.StringBuilder]$sb = [System.Text.StringBuilder]::new()
for:
 [System.Text.StringBuilder]$sb = new-Object System.Text.StringBuilder
0
namergSystems AdministratorAuthor Commented:
PS C:\Scripts> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Open in new window

Same errror:
Output: ==============================================
No files found over 180 minutes old 

Errors: ==============================================
Get-ChildItem : Cannot find path 'C:\Windows\system32\"\SERVER\d$\InetPub_Test\KSFTPWebService' because it does not exist. 
At line:24 char:20 
+ $AllZipFiles = Get-ChildItem -Recurse -Path $path -Include "*.zip" | where{ ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...KSFTPWebService:String) [Get-ChildItem], ItemNotFoundException 
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand 

Open in new window

0
Jose Gabriel Ortega CastroCEOCommented:
how are you running it ?
the command line to run the script  please
0
namergSystems AdministratorAuthor Commented:
Did you see the attachment (screenshot )? I have to paste the script inside the Solarwinds web container and the parameters are passed through it...
0
Jose Gabriel Ortega CastroCEOCommented:
I did see it
The error this time is different:
C:\Windows\system32\"\SERVER\d$\InetPub_Test\KSFTPWebService

it's taking the "C:\Windows\system32\" as preffix and then is attaching this: "\SERVER\d$\InetPub_Test\KSFTPWebService

So the error is not related to the script, it's the path that you're giving it to the solarwinds.
the path should be "\\SERVER\d$\InetPub_Test\KSFTPWebService"
with 2 "\" and with no prefix of "C:\Windows\system32\"
0
namergSystems AdministratorAuthor Commented:
Well, with this one, it works perfect when i execute it from my computer but when i run it through Solarwids, i do not get the error but does not get the results i want.

param([string]$path, [int]$minutes)
$path="\\LFC1KPVCOM01\d$\InetPub_Test\KSFTPWebService\*.zip"
$minutes=180
$stat=0
$msg=""

try
{    
    $LastWrite = $(Get-Date).AddMinutes(-$minutes)  
    $FileArray = $(Get-ChildItem $path | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $LastWrite })
    if ($FileArray.Count -eq 0)
    {
		$msg = "No files found over $($minutes) minutes $($minutes/60) hours old"
    }
    else
    {
        $stat=$FileArray.Count
		$msg = "$($stat) file(s) found older than $($minutes) minutes or $($minutes/60) hours old: "
        foreach ($file in $FileArray)
        {
            $msg+=" " + $file + ","
        }
    }
}
catch
{
    $stat=1
    $msg=$_.Exception.Message
}

Write-Host Statistic: $stat
Write-Host Message: $msg
exit 0

Open in new window

0
Jose Gabriel Ortega CastroCEOCommented:
Yeah I see it doesn't accept the parameter. So let's hard code it.

<#
.Synopsis
   Check 3-hour zip files in path
.DESCRIPTION
   Check 3-hour zip files in path
.EXAMPLE
   Check-ZipsOlderThanX.ps1 -path "D:\path"
.EXAMPLE
   Check-ZipsOlderThanX.ps1 -path "D:\path" -time 240
   (240 minutes => 4 hours
#>
[CmdletBinding()]
param(
    [Parameter(Position=0,Mandatory=$false,ValueFromPipeline=$true)][string[]]$path="\\LFC1KPVCOM01\d$\InetPub_Test\KSFTPWebService",
    [Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$true)]$time=180
)


$Global:ZipFiles=0
[System.Text.StringBuilder]$sb = New-Object System.Text.StringBuilder

try{
    $AllZipFiles = Get-ChildItem -Recurse -Path $path -Include "*.zip" | where{ $_.CreationTime -lt $($(Get-Date).AddMinutes(-$time))}
    $Global:ZipFiles = $AllZipFiles.Count

    if($Global:ZipFiles -gt 0){
       
        foreach($file in $AllZipFiles){
            [void]$sb.AppendLine($file.FullName)
        }
    }
    else{
        Write-Host -ForegroundColor Cyan "No files found over $($time) minutes old"
        break;
    }
}
catch{
    $error=$_.Exception.Message
    Write-Error "There was an error, $error"
    break;
}

Write-Host -ForegroundColor Cyan "There were found $Global:ZipFiles files older than $time minutes on path"
Write-Host -ForegroundColor Gray $($sb.ToString())

Open in new window


Try it like this :)
1
namergSystems AdministratorAuthor Commented:
Jose G, yours worked like a charm. I had to enable just an extra setting in solarwinds. Thanks for your help
1
Jose Gabriel Ortega CastroCEOCommented:
Glad to help sir!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.