-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduledjobs.ps1
140 lines (118 loc) · 3.64 KB
/
scheduledjobs.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
function Get-TaskTrigger {
[cmdletbinding()]
param (
$Task
)
$Triggers = ([xml]$Task.xml).task.Triggers
if ($Triggers) {
$Triggers | Get-Member -MemberType Property | ForEach-Object {
$Triggers.($_.Name)
}
}
}
function Get-scheduledjobz {
[cmdletbinding()]
param (
[string]
$Filter,
[string]
$UserName,
[switch]
$Enabled,
[string]
$TaskName,
[string]
$Trigger,
[string]
$Path,
[string]
$Name
)
begin {
$finalFilter=@()
if ($UserName) {
$userNameString= '$_.userID -eq "$username"'
$finalFilter+=($userNameString)
}
if ($Enabled) {
$finalFilter+='$_.Enabled -eq $True'
}
if ($TaskName) {
$TaskNameString = '$_.Name -eq "$TaskName"'
$finalFilter+=$TaskNameString
}
#Parse through array to create filter for parsing below.
if ($finalFilter.length -gt 1) {
$finalFilter = "{ $($finalFilter -join ' -AND ') }"
Write-Verbose -message "$finalFilter"
}
elseif ( $finalFilter.length -eq 1) {
$finalFilter = $finalFilter[0].ToString()
$finalFilter = $finalFilter -replace "\(",""
$finalFilter = $finalFilter -replace "\)",""
$scriptBlock = [Scriptblock]::Create($finalFilter)
Write-Verbose -Message "ending mutation $finalFilter"
}
else { Remove-Variable finalFilter }
}
process {
$TaskScheduler = New-Object -ComObject "Schedule.Service"
$TaskScheduler.Connect($Null)
$RootFolder = $TaskScheduler.GetFolder('\')
$tasks = $rootFolder.GetTasks(1)
#$tasksxml=@()
$arrayXMLMatches=@()
$arrayFormattedMatches=@()
$Tasks | Foreach-Object {
$currentTask = 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
}
if ($finalFilter) {
$ruleMatch=$currentTask | Where-Object $scriptblock
if ($rulematch) {
#([xml]$_.xml).Task
$arrayXMLMatches+=([xml]$_.xml).Task
$arrayFormattedMatches+=$ruleMatch
Remove-Variable ruleMatch
}
}
else {$currentTask
}
}
if ($arrayXMLMatches -AND $arrayFormattedMatches) {
$arrayXMLMatches
$arrayFormattedMatches
}
}
}
function write-scheduledjobz {
param (
[Parameter(Mandatory=$true)]
[string]
$TaskPath,
[Parameter(Mandatory=$true)]
[string]
$TaskName
)
$GetXMLResults,$GetFormattedResults=Get-ScheduledJobz -Path $TaskPath -TaskName $TaskName
$GetXMLResults.task
}