-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReocurringPSScheduleTask.v0.47.ps1
188 lines (124 loc) · 5.29 KB
/
ReocurringPSScheduleTask.v0.47.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#This Script simplifies the creation scheduled tasks for powershell scripts
#
#region Support mods
function Get-ScriptDirectory
{
if($hostinvocation -ne $null)
{
Split-Path $hostinvocation.MyCommand.path
}
else
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
$SCRIPT_PARENT = Get-ScriptDirectory
$global:ThisScriptFullPath = $MyInvocation.MyCommand.Definition
#Module import
Import-Module "$SCRIPT_PARENT\Modules\Test-Admin.psm1"
Import-Module "$SCRIPT_PARENT\Modules\Elevate.psm1"
#endregion
#Input Vars:
#Powershell Command path
$PSCommand ="$SCRIPT_PARENT\calc.ps1"
#Reoccuring number of minutes
$intervalMInutes=1
#Max execution time in minutes
$execMaxMinutes=60
#Task Name
$TaskName = "Anon WatchList"
#Runlevel Highest or Limited
$RunLevelHigh = $false
#run as constant options
$RunAsLogedOnUser =0;$RunAsSystemUser =1;$RunAsOtherUser =2;
#Chose who to run as
$JobRunAs =$RunAsLogedOnUser
#Do not edit past this point
#Dynamic vars
#interval to repete at.
$RepInterval=(New-TimeSpan -Minutes $intervalMInutes)
#interval to repete at.
$maxExecInterval=(New-TimeSpan -Minutes $execMaxMinutes)
#it can be any previous date or curent date, will do.
$triggerDate=Get-Date
#create trigger(s)
$triggers = @()
#$triggers += New-ScheduledTaskTrigger -RepetitionInterval $RepInterval -Daily -At $triggerDate
$triggers += New-ScheduledTaskTrigger -Once -RepetitionInterval $RepInterval -At $triggerDate
#Starts the job next time is available,
#this is in case the device is unavailable,
#it will run when it is next available
$taskSettings=New-ScheduledTaskSettingsSet
$taskSettings.DisallowStartIfOnBatteries=$false
$taskSettings.ExecutionTimeLimit="PT"+$execMaxMinutes+"M"
$taskSettings.StartWhenAvailable=$true
#region Main code
if(!(Test-Admin)) {
Elevate
}
else
{
$taskResults = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
if(-not [System.String]::IsNullOrEmpty($taskResults))
{$taskExists=$true}
else
{$taskExists=$false}
#Test if task excist
if($taskExists) {
# Do whatever
write-host -nonewline "Task excist, Continue to overwrite ? (Y/N) "
$response = read-host
if ( $response -ne "Y" ) { exit }
}
#Format Command to correct format
$PSCommandReformated = """&$PSCommand""" # format to "&C:\Somefolder\somefile.ps1"
$PSCommandReformated = $PSCommandReformated -replace ' ','` ' #Format to escape spaces
#Action for task
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-NoProfile -WindowStyle Hidden -command $PSCommandReformated"
#clear any default params
$null=$PSDefaultParameterValues.Clear()
#Run High level
if ($RunLevelHigh)
{ $PSDefaultParameterValues.Add(“Register-ScheduledTask:RunLevel”,"Highest") }
#define creds
if ($JobRunAs -eq $RunAsSystemUser)
{
$Principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount
$PSDefaultParameterValues.Add(“Register-ScheduledTask:Principal”,$Principal)
}
elseif($JobRunAs -eq $RunAsLogedOnUser)
{
$UserName = "$env:USERDOMAIN\$env:USERNAME"
$Passrd = Read-Host -assecurestring "Please enter the password for user $UserName"
$Passrd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Passrd ))
$PSDefaultParameterValues.Add(“Register-ScheduledTask:User”,$UserName)
$PSDefaultParameterValues.Add(“Register-ScheduledTask:Password”,$Passrd)
}
elseif($JobRunAs -eq $RunAsOtherUser)
{
$UserName = Read-Host "Please enter a User for task "
$Passrd = Read-Host -assecurestring "Please enter the password for user $UserName"
$Passrd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Passrd ))
$PSDefaultParameterValues.Add(“Register-ScheduledTask:User”,$UserName)
$PSDefaultParameterValues.Add(“Register-ScheduledTask:Password”,$Passrd)
}
#create task
$TaskResult =Register-ScheduledTask -Action $action -Trigger $triggers -TaskName $TaskName -Force -Settings $taskSettings
Write-Host "-----------------------------"
if (!$TaskResult)
{
write-host "Task was NOT created!!!"
}
else{
Write-Host "The Task with the following details was successfully created"
Write-Host "`tTaskResult.TaskName:`t`t`t $($TaskResult.TaskName)"
Write-Host "`tTaskResult.State:`t`t`t`t $($TaskResult.State)"
Write-Host "`tTaskResult.State:`t`t`t`t $($TaskResult.State)"
Write-Host "`tTaskResult.Actions.Execute:`t`t $($TaskResult.Actions.Execute)"
Write-Host "`tTaskResult.Actions.Arguments:`t $($TaskResult.Actions.Arguments)"
Write-Host "`tTaskResult.Principal.UserId:`t $($TaskResult.Principal.UserId)"
Write-Host "`tTaskResult.Principal.RunLevel:`t $($TaskResult.Principal.RunLevel)"
Write-Host "`tTaskResult.Principal.LogonType:`t $($TaskResult.Principal.LogonType)"
}
}
#endregion