Functions/Validations/Test-PathLifetime.ps1
<#
.SYNOPSIS Tests if the lifetime of a directory or file, based on the creationtime or the lastwritetime, is lower than the set maximum age (TotalMinutes). .DESCRIPTION This function returns $true if the item exists and if the lifetime of the item is below the set threshold. If the item on the path does not exists or if the item is older than the set threshold an error is written and the function returns $false. .EXAMPLE Test-PathLifetime -Path c:\temp\ .EXAMPLE Test-PathLifetime -Path c:\temp\test.txt -TotalMinutes 15 -LastWriteTime .EXAMPLE Test-PathLifetime -Path c:\temp\test.txt -TotalMinutes 15 -ErrorAction Stop .EXAMPLE @('c:\temp\testA.txt', c:\temp\testB.txt) | Test-PathLifetime -TotalMinutes 10 #> function Test-PathLifetime { [cmdletbinding()] [OutputType([bool])] Param ( # The path to test the lifetime for. [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] $Path, # The maximum age of the item to pass this test. [Parameter(Mandatory=$false)] $TotalMinutes = '5', # Default this function validates the CreationTime of the item. # In some cases if an item is deleted and recreated in quick succession the CreationTime is not reset. # In that case a test on LastWriteTime could be used. [switch] $LastWriteTime ) # Test if the path exists if ((Test-Path $Path) -ne $true){ $ErrorMessage = "Could not get item {0} on the path '{1}', no item was found on path '{2}'" -f ` $(if( $LastWriteTime ){ 'LastWriteTime' } else { 'CreationTime' } ), ($Path | Split-Path -Leaf), $Path Write-Error $ErrorMessage return $false } # Create a timespan between the item creation time or last write time and the current time. $TimeSpan = New-TimeSpan ` -Start (Get-ItemProperty $Path | Select-Object -Property CreationTimeUtc).CreationTimeUtc ` -End (Get-Date).ToUniversalTime() if ($LastWriteTime) { $TimeSpan = New-TimeSpan ` -Start (Get-ItemProperty $Path | Select-Object -Property LastWriteTimeUtc).LastWriteTimeUtc ` -End (Get-Date).ToUniversalTime() } # Test if the time difference between the creationtime/lastwritetime is lower than the set threshold of TotalMinutes. if ($TimeSpan.TotalMinutes -gt $TotalMinutes) { $ErrorMessage = "The {0} of the item '{1}' is older than the set threshold of {2} minutes. The item not recent enough. ` Item location = {3} CurrentTime = {4} CreationTime = {5} LastWriteTime = {6}" -f ` $(if( $LastWriteTime ){ 'LastWriteTime' } else { 'CreationTime' } ), ($Path | Split-Path -Leaf), $TotalMinutes, $Path, (Get-Date), ((Get-ItemProperty $Path | Select-Object -Property CreationTime).CreationTime), ((Get-ItemProperty $Path | Select-Object -Property LastWriteTime).LastWriteTime) Write-Error $ErrorMessage return $false } # Item exists and passed the lifetime test. $Message = "The {0} of the item '{1}' is within the set threshold of {2} minutes. ` Item location = {3} CurrentTime = {4} CreationTime = {5} LastWriteTime = {6}" -f ` $(if( $LastWriteTime ){ 'LastWriteTime' } else { 'CreationTime' } ), ($Path | Split-Path -Leaf), $TotalMinutes, $Path, (Get-Date), ((Get-ItemProperty $Path | Select-Object -Property CreationTime).CreationTime), ((Get-ItemProperty $Path | Select-Object -Property LastWriteTime).LastWriteTime) Write-Verbose $Message $true } Export-ModuleMember -Function Test-PathLifetime |