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

How To Link Containers Using Docker Compose

This yields several advantages:1. Direct linking between containers is architectually efficient to direct traffic between…

Add a Domain Group to Local Administrators Group

$checkGroup="Administrators" $addMember="KIMCONNECT\Desktop Admins" # Dynamic Credential $who = whoami if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;} else…

PowerShell: Function to Execute Function Remotely

Function this function that - this quick snippet is to invoke those func things remotely...…