-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZT-ScheduledTasks.py
145 lines (121 loc) · 5.24 KB
/
EZT-ScheduledTasks.py
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
import os
import sys
ps_content=r'''
<#
.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 = "''' + itsm.getParameter('Check_Task_Name') + '''"
$Task_MatchAny_Name = ([System.Convert]::ToBoolean(''' + itsm.getParameter('Task_MatchAny_Name') + ''')) #The matching name of the scheduled task to search for
$Task_StartsWith_Name = ([System.Convert]::ToBoolean(''' + itsm.getParameter('Task_StartsWith_Name') + '''))
$Task_Exact_Name = ([System.Convert]::ToBoolean(''' + itsm.getParameter('Task_Exact_Name') + ''')) #The exact name of the scheduled task to search for
$Enable_Task = ([System.Convert]::ToBoolean(''' + itsm.getParameter('Enable_Task') + ''')) #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)"
write-output " | Task Description: $($task.Description)"
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
#############################################################################
'''
print ("iTarian RMM - Executing Powershell Script")
def ecmd(command):
import ctypes
from subprocess import PIPE, Popen
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
with disable_file_system_redirection():
obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
out, err = obj.communicate()
ret=obj.returncode
if ret==0:
if out:
return out.strip()
else:
return ret
else:
if err:
return err.strip()
else:
return ret
file_name='EZT-ScheduledTasks.ps1'
file_path=os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
ecmd('powershell "Set-ExecutionPolicy Bypass"')
print ecmd('powershell "%s"'%file_path)
os.remove(file_path)