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."
}
}
Here is a shorter and probably more readable implementation:
$Path = "C:\Users\<username>\Desktop\Archive\*"
$Date = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
$Hours = Get-ChildItem -Path $Path -Include "850_$($Date)*.txt" | ForEach-Object {(Get-Date $_.CreationTime).Hour} | Sort-Object | Get-Unique
for ($Hour = 0; $Hour -lt 24; $Hour++) {
if ($Hour -in $Hours) {
Write-Host "There's at least one file for $Hour o'clock for $Date." -ForegroundColor Green
} else {
Write-Host "There's no file present for $Hour o'clock for $Date." -ForegroundColor Red
}
}
So what have I changed:
params
block as I think you just used it to initialize variables, but it is designed to define parameters you want pass to a function. It does not look like you want to pass anything to that script. Correct me, if I'm wrong.*
) to your path to make the -Include
parameter work (see docs). I think you added -Recurse
to make it work. Use -Recurse
only if your logs are located in subdirectories.