PowerShell: Check Windows Scheduled Tasks To Correlate Object Creation Time of a Zip File

$file='c:\temp\backup.zip'
$computername='SERVER008'
$minutesVariance=15

checkScheduledTaskMatchingFile $file $computername $minutesVariance

function checkScheduledTaskMatchingFile{
    param(
        $file='c:\temp\PROD.zip',
        $computername=$env:computername,
        $minutesVariance=15
    )
    if($computername -eq $env:computername){
        $lastWriteTime=(get-itemproperty $file).LastWriteTime
        $allActiveTasks=Get-ScheduledTask|Get-ScheduledTaskInfo|?{$_.LastRunTime}
        $result=$allActiveTasks|?{$_.LastRunTime -le $lastWriteTime.AddMinutes($minutesVariance) -and $_.LastRunTime -ge $lastWriteTime.AddMinutes(-$minutesVariance)}
        if($result){
            return $result
        }else{
            write-warning "NO Scheduled tasks on $env:computername matching the timing of $file, which has a LastWriteTime of $(($lastWriteTime|out-string).trim())"
            return $null    
        }
    }else{
        $lastWriteTime=invoke-command -computername $computername {param($file);(get-itemproperty $file).LastWriteTime} -Args $file
        $allActiveTasks=invoke-command -computername $computername {Get-ScheduledTask|Get-ScheduledTaskInfo|?{$_.LastRunTime}}
        $result=$allActiveTasks|?{$_.LastRunTime -le $lastWriteTime.AddMinutes($minutesVariance) -and $_.LastRunTime -ge $lastWriteTime.AddMinutes(-$minutesVariance)}
        if($result){
            return $result
        }else{
            write-warning "NO Scheduled tasks on $computername matching the timing of $file, which has a LastWriteTime of $(($lastWriteTime|out-string).trim())"
            return $null    
        }        
    }
}

Leave a Reply

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