-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZT-ScheduledTasks.ps1
99 lines (82 loc) · 3.76 KB
/
EZT-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
<#
.Name
EZT-ScheduledTasks
.Version
0.1.0
.SYNOPSIS
Checks for scheduled tasks by name and enables or disables them
.DESCRIPTION
.Configurable Variables
.Requirements
- Powershell v3.0 or higher
.EXAMPLE
.\EZT-ScheduledTasks.ps1
.OUTPUTS
System.Management.Automation.PSObject
.NOTES
Author: EZTechhelp
Site : https://www.eztechhelp.com
#>
#############################################################################
#region Configurable Script Parameters
#############################################################################
#----------------------------------------------
#region Function Select Variables
#----------------------------------------------
$Check_Task_Name = 'Google' #The name of the scheduled task to search for
$Task_MatchAny_Name = ([System.Convert]::ToBoolean(0)) #Find any tasks matching name
$Task_StartsWith_Name = ([System.Convert]::ToBoolean(1)) #Find tasks starting with name
$Task_Exact_Name = ([System.Convert]::ToBoolean(0)) #Find tasks with exact name
$Enable_Task = ([System.Convert]::ToBoolean(0)) #Enable (1) or Disables (0) all tasks matching provide name
#----------------------------------------------
#endregion Function Select Variables
#----------------------------------------------
#############################################################################
#endregion Configurable Script Parameters
#############################################################################
#############################################################################
#region Execution and Output - Functions or Code that executes required actions and/or performs output
#############################################################################
#----------------------------------------------
#region Schedule Tasks
#----------------------------------------------
if($Task_MatchAny_Name){
$Task_Name = "*$Check_Task_Name*"
write-output "#### Checking for scheduled tasks matching name $Task_Name ####"
$scheduled_Task = (Get-scheduledtask -taskname "$Task_Name") | select * | where {$_.settings.Enabled -ne $Enable_Task}
}elseif($Task_StartsWith_Name){
$Task_Name = "$Check_Task_Name*"
write-output "#### Checking for scheduled tasks starting with name $Task_Name ####"
$scheduled_Task = (Get-scheduledtask -taskname "$Task_Name") | select * | where {$_.settings.Enabled -ne $Enable_Task}
}elseif($Task_Exact_Name){
$Task_Name = $Check_Task_Name
write-output "#### Checking for scheduled tasks with exact name $Task_Name ####"
$scheduled_Task = (Get-scheduledtask -taskname "$Task_Name") | select * | where {$_.settings.Enabled -ne $Enable_Task}
}else{
write-warning "You must provide a task name to search for"
exit
}
if($scheduled_Task){
foreach($task in $scheduled_Task){
write-output "`n>>>> Found Scheduled Tasks matching search criteria"
write-output " | Task Name: $($task.taskname)"
write-output " | Task State: $($task.State)"
if($Enable_Task -and !$task.settings.Enabled){
write-output ">>>> Enabling Task $($task.taskname)"
$null = Enable-ScheduledTask -TaskName $task.taskname
}elseif(!$Enable_Task -and $task.settings.Enabled){
write-output ">>>> Disabling Task $($task.taskname)"
$null = Disable-ScheduledTask -TaskName $task.taskname
}else{
write-output "No matching scheduled tasks were modified"
}
}
}else{
write-output "No Scheduled Tasks found matching name $Task_Name"
}
#----------------------------------------------
#endregion Schedule Tasks
#----------------------------------------------
#############################################################################
#endregion Execution and Output Functions
#############################################################################