Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed recursion logic and hidden tasks #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 45 additions & 50 deletions Get-ScheduledTasks.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Get-ScheduledTasks {
function Get-ScheduledTasks {
<#
.SYNOPSIS
Get scheduled task information from a system
Expand Down Expand Up @@ -69,6 +69,7 @@
RunLevel : COM object queries
Description : COM object queries
NumberOfMissedRuns : COM object queries
XML : COM object queries

Thanks to help from Brian Wilhite, Jaap Brasser, and Jan Egil's functions:
http://gallery.technet.microsoft.com/scriptcenter/Get-SchedTasks-Determine-5e04513f
Expand Down Expand Up @@ -127,52 +128,45 @@
param (
# Set to use $Schedule as default parameter so it automatically list all files
# For current schedule object if it exists.
$FolderRef = $sch.getfolder("\"),

[switch]$recurse
$FolderRef = $sch.getfolder("\")
)

#No recurse? Return the folder reference
if (-not $recurse) {
$FolderRef
}
#Recurse? Build up an array!
else {
Try{
#This will fail on older systems...
$folders = $folderRef.getfolders(1)

#Extract results into array
$ArrFolders = @(
if($folders) {
foreach ($fold in $folders) {
$fold
if($fold.getfolders(1)) {
Get-AllTaskSubFolders -FolderRef $fold
}
Try{
#This will fail on older systems...
$folders = $folderRef.getfolders(1)

#Extract results into array
$ArrFolders = @(
if($folders) {
foreach ($fold in $folders) {
$fold
if($fold.getfolders(1)) {
Get-AllTaskSubFolders -FolderRef $fold
}
}
)
}
Catch{
#If we failed and the expected error, return folder ref only!
if($_.tostring() -like '*Exception calling "GetFolders" with "1" argument(s): "The request is not supported.*')
{
$folders = $null
Write-Warning "GetFolders failed, returning root folder only: $_"
Return $FolderRef
}
else{
Throw $_
}
)
}
Catch{
#If we failed and the expected error, return folder ref only!
if($_.tostring() -like '*Exception calling "GetFolders" with "1" argument(s): "The request is not supported.*')
{
$folders = $null
Write-Warning "GetFolders failed, returning root folder only: $_"
Return $FolderRef
}
else{
Throw $_
}

#Return only unique results
$Results = @($ArrFolders) + @($FolderRef)
$UniquePaths = $Results | select -ExpandProperty path -Unique
$Results | ?{$UniquePaths -contains $_.path}
}

#Return only unique results
$Results = @($ArrFolders)
$UniquePaths = $Results | Sort-Object -Property path | Select-Object -ExpandProperty path -Unique
$Results | ?{$UniquePaths -contains $_.path}

} #Get-AllTaskSubFolders

}

function Get-SchTasks {
Expand Down Expand Up @@ -224,20 +218,20 @@
#Connect to the computer
$sch.Connect($computer)

if($recurse)
{
$AllFolders = Get-AllTaskSubFolders -FolderRef $sch.GetFolder($folder) -recurse -ErrorAction stop
}
else
{
$AllFolders = Get-AllTaskSubFolders -FolderRef $sch.GetFolder($folder) -ErrorAction stop
$AllFolders = @()
$AllFolders += $sch.GetFolder($folder)

if($recurse) {
$AllFolders += Get-AllTaskSubFolders -FolderRef $sch.GetFolder($folder) -ErrorAction stop
}


Write-verbose "Looking through $($AllFolders.count) folders on $computer"

foreach($fold in $AllFolders){

#Get tasks in this folder
$tasks = $fold.GetTasks(0)
$tasks = $fold.GetTasks(1) # 1 = get hidden tasks, too: https://docs.microsoft.com/en-us/windows/win32/api/taskschd/nf-taskschd-itaskfolder-gettasks

Write-Verbose "Pulling data from $($tasks.count) tasks on $computer in $($fold.name)"
foreach($task in $tasks){
Expand Down Expand Up @@ -275,8 +269,9 @@
@{ label = "Author"; expression = {$Author} },
@{ label = "RunLevel"; expression = {$RunLevel} },
@{ label = "Description"; expression = {$Description} },
NumberOfMissedRuns

NumberOfMissedRuns,
@{ label = "XML"; expression = { try { $([xml]$task.Xml).Task } catch {$null} } }

#if specified, output the results in importable XML format
if($path){
$xml = $task.Xml
Expand Down Expand Up @@ -318,4 +313,4 @@
}
}
}
}
}