Posted On October 20, 2021

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

Bash Shell: Old School Migration of ESXi Guest Virtual Machines

Step 0: Preparations Estimate file-copying duration for cut-over:- Sustained storage transfer speed using a 1GB…

PowerShell: Uninstalling an Application

$appname='Google Chrome' function uninstallApp($appName){ $alternativeMethod=$false try{ $app = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq $appName}…

Manual Sync for Office 365 Azure-AD Integration

1. Perform Prerequisites:* a. Install Dot Net Framework 4.5 )* b. Install Windows Management Framework…
Other project: DragonCoin.com