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: How To Invoke Rest Method with RingCentral Rest API

[string]$restApiTokenUrl="https://platform.ringcentral.com/restapi/oauth/token" [string]$restApiUrl="https://platform.ringcentral.com/restapi/v1.0/account/~/extension?page=1" [string]$username='15555555555' [string]$password='PASSWORDHERE' [string]$extension='100' [string]$appKey='APPKEYHERE' # Part 1: Obtain Rest-API Token / Authorization function…

PowerShell: Get Quorums of All Clusters in the Domain

# getClusterQuorum.ps1 function getClusterQuorum{ $allClusters=(get-cluster -domain $env:USERDNSDOMAIN).Name $results=@{} foreach ($cluster in $allClusters){ $quorum=Get-ClusterQuorum -Cluster $cluster…

PowerShell: List Currently Logon Users On Remote Servers

# Show current sessions on a list of servers$servers="SHERVER005","SHERVER007";$servers|%{"$_`n$(query user /server:$_|Out-String)"} # Sample OutputPS C:\Windows\system32>…