-
Notifications
You must be signed in to change notification settings - Fork 51
/
Get-ScheduledTasks.ps1
145 lines (122 loc) · 4.44 KB
/
Get-ScheduledTasks.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<#
.SYNOPSIS
Script that returns scheduled tasks on a computer
.DESCRIPTION
This script uses the Schedule.Service COM-object to query the local or a remote computer in order to gather a formatted list including the Author, UserId and description of the task. This information is parsed from the XML attributed to provide a more human readable format
.PARAMETER Computername
The computer that will be queried by this script, local administrative permissions are required to query this information
.NOTES
Name: Get-ScheduledTask.ps1
Author: Jaap Brasser
DateCreated: 2012-05-23
DateUpdated: 2015-08-17
Site: http://www.jaapbrasser.com
Version: 1.3.2
.LINK
http://www.jaapbrasser.com
.EXAMPLE
.\Get-ScheduledTask.ps1 -ComputerName server01
Description
-----------
This command query mycomputer1 and display a formatted list of all scheduled tasks on that computer
.EXAMPLE
.\Get-ScheduledTask.ps1
Description
-----------
This command query localhost and display a formatted list of all scheduled tasks on the local computer
.EXAMPLE
.\Get-ScheduledTask.ps1 -ComputerName server01 | Select-Object -Property Name,Trigger
Description
-----------
This command query server01 for scheduled tasks and display only the TaskName and the assigned trigger(s)
.EXAMPLE
.\Get-ScheduledTask.ps1 | Where-Object {$_.Name -eq 'TaskName') | Select-Object -ExpandProperty Trigger
Description
-----------
This command queries the local system for a scheduled task named 'TaskName' and display the expanded view of the assisgned trigger(s)
.EXAMPLE
Get-Content C:\Servers.txt | ForEach-Object { .\Get-ScheduledTask.ps1 -ComputerName $_ }
Description
-----------
Reads the contents of C:\Servers.txt and pipes the output to Get-ScheduledTask.ps1 and outputs the results to the console
#>
param(
[string]$ComputerName = $env:COMPUTERNAME,
[switch]$RootFolder,
[string]$csvpath = $PSScriptRoot,
[string]$csvname = "$ComputerName-Scheduled Tasks.csv"
)
#region Functions
function Get-AllTaskSubFolders {
[cmdletbinding()]
param (
# Set to use $Schedule as default parameter so it automatically list all files
# For current schedule object if it exists.
$FolderRef = $Schedule.getfolder("\")
)
if ($FolderRef.Path -eq '\') {
$FolderRef
}
if (-not $RootFolder) {
$ArrFolders = @()
if(($Folders = $folderRef.getfolders(1))) {
$Folders | ForEach-Object {
$ArrFolders += $_
if($_.getfolders(1)) {
Get-AllTaskSubFolders -FolderRef $_
}
}
}
$ArrFolders
}
}
function Get-TaskTrigger {
[cmdletbinding()]
param (
$Task
)
$Triggers = ([xml]$Task.xml).task.Triggers
if ($Triggers) {
$Triggers | Get-Member -MemberType Property | ForEach-Object {
$Triggers.($_.Name)
}
}
}
#endregion Functions
try {
$Schedule = New-Object -ComObject 'Schedule.Service'
} catch {
Write-Warning "Schedule.Service COM Object not found, this script requires this object"
return
}
$Schedule.connect($ComputerName)
$AllFolders = Get-AllTaskSubFolders
foreach ($Folder in $AllFolders) {
if (($Tasks = $Folder.GetTasks(1))) {
$Tasks | Foreach-Object {
$obj = New-Object -TypeName PSCustomObject -Property @{
'Name' = $_.name
'Path' = $_.path
'State' = switch ($_.State) {
0 {'Unknown'}
1 {'Disabled'}
2 {'Queued'}
3 {'Ready'}
4 {'Running'}
Default {'Unknown'}
}
'Enabled' = $_.enabled
'LastRunTime' = $_.lastruntime
'LastTaskResult' = $_.lasttaskresult
'NumberOfMissedRuns' = $_.numberofmissedruns
'NextRunTime' = $_.nextruntime
'Author' = ([xml]$_.xml).Task.RegistrationInfo.Author
'UserId' = ([xml]$_.xml).Task.Principals.Principal.UserID
'Description' = ([xml]$_.xml).Task.RegistrationInfo.Description
'Trigger' = Get-TaskTrigger -Task $_
'ComputerName' = $Schedule.TargetServer
}
$Obj |Export-Csv "$CSVPath\$csvname" -NoTypeInformation -Append
}
}
}