I'm attempting to create a PowerShell script that will run once every morning (via Task Scheduler) and alert someone (via Direct Send (I know how to do this and will implement said feature later)) if a file wasn't created during each hour for the day before, in order to no longer have to do this manually or when a problem arises. The filenames, as shown in the $FilePattern, all contain the date they're created, along with the time. Obviously, the logic in my Foreach loop is flawed, because $Time will always be in $Hours, but I've been scratching my head and haven't been able to figure out how I should go about this. I thought I'd be able to compare two arrays, but I'm not so sure now. I also ran across Compare-Object which seemed promising, but I couldn't quite grasp it. My apologies for the mess. My current code is as follows.
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[ValidateScript({ Test-Path $_ -PathType Container })]
[string]
$Path = "C:\Users\<username>\Desktop\Archive",
$Year = [DateTime]::Today.AddDays(-1).Year,
$Month = [DateTime]::Today.AddDays(-1).Month,
$Day = ([DateTime]::Today.AddDays(-1).Day),
$strMonth = ([String]$Month).PadLeft(2,'0'),
$strDay = ([String]$Day).PadLeft(2,'0'),
$Date = "$Year" + "$strMonth" + "$strDay",
$FilePattern = @("850_" + $Date + "*.txt"),
$Files = (Get-ChildItem -Path $Path -Include $FilePattern -Recurse |
Select-Object -ExpandProperty CreationTime | Get-Date -f "yyyyMMdd"),
$Time = (Get-ChildItem -Path $Path -Include $FilePattern -Recurse |
Select-Object -ExpandProperty CreationTime | Get-Date -f "hh"),
$Hours = @(0..23)
)
Foreach ($File in $Files) {
#if (-Not (Test-Path "$Path\$FilePattern")) {
# Write-Host "Warning: The file path doesn't exist!"
#} else {
# Write-Host "The file path exists..."
#}
if ($Hours -notin $Time) {
Write-Host "There's no file present for $Time o'clock for $Date."
} else {
Write-Host "There's at least one file per hour for $Date."
}
}