Tuesday, January 16, 2018

Powershell script to find all scheduled tasks

$erroractionPreference="stop"
$pcs = Get-ADComputer -Filter "Name -like '*'" | select-object name
foreach ($pc in $pcs) {
    $computername = $pc.name
    if(Test-Connection -ComputerName $computername -Count 1 -Quiet){
        Try
        {
            $path = "\\" + $computername + "\c$\Windows\System32\Tasks"
            $tasks = Get-ChildItem -Path $path -File
            foreach($task in $tasks){
                $str = $computername + "," + $task
                Write-Host $str   
            }
        }
        Catch
        {
            $str = $computername + ","
            Write-Host $str
        }
    }
    else{
        $str = $computername + ","
        Write-Host $str
    }

}

No comments:

Post a Comment