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

PowerShell: Add New Virtual Disk to Existing Guest VM in Hyper-V

# Adding disks (optional) $newVMNames='TestWindows2019' $extraDiskSize='200GB' if($extraDiskSize){ foreach($newVmName in $newVMNames){ STOP-VM -vmname $newVmName $diskFile=(join-path $destinationFolder…

PowerShell: Enable TLS 1.2 on legacy servers

# Enable TLS 1.2 on legacy servers function enableTls12{ $windowsVersionNumber=[System.Environment]::OSVersion.Version.Major; #$windowsName=(Get-CimInstance -ClassName Win32_OperatingSystem).Caption; #The term…

Hyper-V: Cloning a Virtual Machine

Update 11/25/2021: There's anther variation of this function at: https://blog.kimconnect.com/powershell-create-hyper-v-guest-vm-from-virtual-disk-vhdx/This function will dynamically detect source…
Other project: DragonCoin.com