This might take you < 1 minute to read.

Ever wanted to keep track of if the latest file in a folder is younger than a specific time? I wanted to know if a service created a backup-file each week. This script is triggered a couple of times a day and will create a file that a surveillance system pick up.

$path = "c:pathtocheck"
$larmHours = 168
$OutputFile = "c:tempfilecheck.txt"

function getFileAge {
	param (
		[parameter(Mandatory=$True)]
		[string]$path,
		[int]$larmHours=7
		)

	$item = Get-ChildItem ($path) | sort LastWriteTime | select -last 1
	if ( $item.LastWriteTime -lt ((Get-Date).AddHours(-$larmHours)) ) {
		$value = "red"
	} else {
		$value = "green"
	}

	$return = $value + "`r`n"
	$return += "&" + $value + " " + $item.FullName + ", " + $item.lastwritetime
	return $return
}

add-content $OutputFile (GetFileAge -path $path -larmHours $larmHours)

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.