| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Peter Mounce <[email protected]> |
| 4 |
# Copyright: (c) 2015, Michael Perzel <[email protected]> |
| 5 |
# Copyright: (c) 2017, Ansible Project |
| 6 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
#AnsibleRequires -PowerShell Ansible.ModuleUtils.AddType |
| 9 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 10 |
#Requires -Module Ansible.ModuleUtils.SID |
| 11 |
|
| 12 |
$ErrorActionPreference = "Stop" |
| 13 |
|
| 14 |
$params = Parse-Args -arguments $args -supports_check_mode $true |
| 15 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 16 |
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false |
| 17 |
$_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP |
| 18 |
|
| 19 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 20 |
$path = Get-AnsibleParam -obj $params -name "path" -type "str" -default "\" |
| 21 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent", "present" |
| 22 |
|
| 23 |
# task actions, list of dicts [{path, arguments, working_directory}] |
| 24 |
$actions = Get-AnsibleParam -obj $params -name "actions" -type "list" |
| 25 |
|
| 26 |
# task triggers, list of dicts [{ type, ... }] |
| 27 |
$triggers = Get-AnsibleParam -obj $params -name "triggers" -type "list" |
| 28 |
|
| 29 |
# task Principal properties |
| 30 |
$display_name = Get-AnsibleParam -obj $params -name "display_name" -type "str" |
| 31 |
$group = Get-AnsibleParam -obj $params -name "group" -type "str" |
| 32 |
$logon_options = "none", "password", "s4u", "interactive_token", "group", "service_account", "interactive_token_or_password" |
| 33 |
$logon_type = Get-AnsibleParam -obj $params -name "logon_type" -type "str" -validateset $logon_options |
| 34 |
$run_level = Get-AnsibleParam -obj $params -name "run_level" -type "str" -validateset "limited", "highest" -aliases "runlevel" |
| 35 |
$username = Get-AnsibleParam -obj $params -name "username" -type "str" -aliases "user" |
| 36 |
$password = Get-AnsibleParam -obj $params -name "password" -type "str" |
| 37 |
$update_password = Get-AnsibleParam -obj $params -name "update_password" -type "bool" -default $true |
| 38 |
|
| 39 |
# task RegistrationInfo properties |
| 40 |
$author = Get-AnsibleParam -obj $params -name "author" -type "str" |
| 41 |
$date = Get-AnsibleParam -obj $params -name "date" -type "str" |
| 42 |
$description = Get-AnsibleParam -obj $params -name "description" -type "str" |
| 43 |
$source = Get-AnsibleParam -obj $params -name "source" -type "str" |
| 44 |
$version = Get-AnsibleParam -obj $params -name "version" -type "str" |
| 45 |
|
| 46 |
# task Settings properties |
| 47 |
$allow_demand_start = Get-AnsibleParam -obj $params -name "allow_demand_start" -type "bool" |
| 48 |
$allow_hard_terminate = Get-AnsibleParam -obj $params -name "allow_hard_terminate" -type "bool" |
| 49 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383486(v=vs.85).aspx |
| 50 |
$compatibility = Get-AnsibleParam -obj $params -name "compatibility" -type "int" |
| 51 |
$delete_expired_task_after = Get-AnsibleParam -obj $params -name "delete_expired_task_after" -type "str" # time string PT... |
| 52 |
$disallow_start_if_on_batteries = Get-AnsibleParam -obj $params -name "disallow_start_if_on_batteries" -type "bool" |
| 53 |
$enabled = Get-AnsibleParam -obj $params -name "enabled" -type "bool" |
| 54 |
$execution_time_limit = Get-AnsibleParam -obj $params -name "execution_time_limit" -type "str" # PT72H |
| 55 |
$hidden = Get-AnsibleParam -obj $params -name "hidden" -type "bool" |
| 56 |
# TODO: support for $idle_settings, needs to be created as a COM object |
| 57 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383507(v=vs.85).aspx |
| 58 |
$multiple_instances = Get-AnsibleParam -obj $params -name "multiple_instances" -type "int" |
| 59 |
# TODO: support for $network_settings, needs to be created as a COM object |
| 60 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383512(v=vs.85).aspx |
| 61 |
$priority = Get-AnsibleParam -obj $params -name "priority" -type "int" |
| 62 |
$restart_count = Get-AnsibleParam -obj $params -name "restart_count" -type "int" |
| 63 |
$restart_interval = Get-AnsibleParam -obj $params -name "restart_interval" -type "str" # time string PT.. |
| 64 |
$run_only_if_idle = Get-AnsibleParam -obj $params -name "run_only_if_idle" -type "bool" |
| 65 |
$run_only_if_network_available = Get-AnsibleParam -obj $params -name "run_only_if_network_available" -type "bool" |
| 66 |
$start_when_available = Get-AnsibleParam -obj $params -name "start_when_available" -type "bool" |
| 67 |
$stop_if_going_on_batteries = Get-AnsibleParam -obj $params -name "stop_if_going_on_batteries" -type "bool" |
| 68 |
$wake_to_run = Get-AnsibleParam -obj $params -name "wake_to_run" -type "bool" |
| 69 |
|
| 70 |
$result = @{ |
| 71 |
changed = $false |
| 72 |
} |
| 73 |
|
| 74 |
if ($diff_mode) { |
| 75 |
$result.diff = @{} |
| 76 |
} |
| 77 |
|
| 78 |
Add-CSharpType -TempPath $_remote_tmp -References @' |
| 79 |
public enum TASK_ACTION_TYPE // https://msdn.microsoft.com/en-us/library/windows/desktop/aa383553(v=vs.85).aspx |
| 80 |
{ |
| 81 |
TASK_ACTION_EXEC = 0, |
| 82 |
// The below are not supported and are only kept for documentation purposes |
| 83 |
TASK_ACTION_COM_HANDLER = 5, |
| 84 |
TASK_ACTION_SEND_EMAIL = 6, |
| 85 |
TASK_ACTION_SHOW_MESSAGE = 7 |
| 86 |
} |
| 87 |
|
| 88 |
public enum TASK_CREATION // https://msdn.microsoft.com/en-us/library/windows/desktop/aa382538(v=vs.85).aspx |
| 89 |
{ |
| 90 |
TASK_VALIDATE_ONLY = 0x1, |
| 91 |
TASK_CREATE = 0x2, |
| 92 |
TASK_UPDATE = 0x4, |
| 93 |
TASK_CREATE_OR_UPDATE = 0x6, |
| 94 |
TASK_DISABLE = 0x8, |
| 95 |
TASK_DONT_ADD_PRINCIPAL_ACE = 0x10, |
| 96 |
TASK_IGNORE_REGISTRATION_TRIGGERS = 0x20 |
| 97 |
} |
| 98 |
|
| 99 |
public enum TASK_LOGON_TYPE // https://msdn.microsoft.com/en-us/library/windows/desktop/aa383566(v=vs.85).aspx |
| 100 |
{ |
| 101 |
TASK_LOGON_NONE = 0, |
| 102 |
TASK_LOGON_PASSWORD = 1, |
| 103 |
TASK_LOGON_S4U = 2, |
| 104 |
TASK_LOGON_INTERACTIVE_TOKEN = 3, |
| 105 |
TASK_LOGON_GROUP = 4, |
| 106 |
TASK_LOGON_SERVICE_ACCOUNT = 5, |
| 107 |
TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD = 6 |
| 108 |
} |
| 109 |
|
| 110 |
public enum TASK_RUN_LEVEL // https://msdn.microsoft.com/en-us/library/windows/desktop/aa380747(v=vs.85).aspx |
| 111 |
{ |
| 112 |
TASK_RUNLEVEL_LUA = 0, |
| 113 |
TASK_RUNLEVEL_HIGHEST = 1 |
| 114 |
} |
| 115 |
|
| 116 |
public enum TASK_TRIGGER_TYPE2 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa383915(v=vs.85).aspx |
| 117 |
{ |
| 118 |
TASK_TRIGGER_EVENT = 0, |
| 119 |
TASK_TRIGGER_TIME = 1, |
| 120 |
TASK_TRIGGER_DAILY = 2, |
| 121 |
TASK_TRIGGER_WEEKLY = 3, |
| 122 |
TASK_TRIGGER_MONTHLY = 4, |
| 123 |
TASK_TRIGGER_MONTHLYDOW = 5, |
| 124 |
TASK_TRIGGER_IDLE = 6, |
| 125 |
TASK_TRIGGER_REGISTRATION = 7, |
| 126 |
TASK_TRIGGER_BOOT = 8, |
| 127 |
TASK_TRIGGER_LOGON = 9, |
| 128 |
TASK_TRIGGER_SESSION_STATE_CHANGE = 11 |
| 129 |
} |
| 130 |
|
| 131 |
public enum TASK_SESSION_STATE_CHANGE_TYPE // https://docs.microsoft.com/en-us/windows/win32/api/taskschd/ne-taskschd-task_session_state_change_type |
| 132 |
{ |
| 133 |
TASK_CONSOLE_CONNECT = 1, |
| 134 |
TASK_CONSOLE_DISCONNECT = 2, |
| 135 |
TASK_REMOTE_CONNECT = 3, |
| 136 |
TASK_REMOTE_DISCONNECT = 4, |
| 137 |
TASK_SESSION_LOCK = 7, |
| 138 |
TASK_SESSION_UNLOCK = 8 |
| 139 |
} |
| 140 |
'@ |
| 141 |
|
| 142 |
######################## |
| 143 |
### HELPER FUNCTIONS ### |
| 144 |
######################## |
| 145 |
Function Convert-SnakeToPascalCase($snake) { |
| 146 |
# very basic function to convert snake_case to PascalCase for use in COM |
| 147 |
# objects |
| 148 |
[regex]$regex = "_(\w)" |
| 149 |
$pascal_case = $regex.Replace($snake, { $args[0].Value.Substring(1).ToUpper() }) |
| 150 |
$capitalised = $pascal_case.Substring(0, 1).ToUpper() + $pascal_case.Substring(1) |
| 151 |
|
| 152 |
return $capitalised |
| 153 |
} |
| 154 |
|
| 155 |
Function Compare-Property($property_name, $parent_property, $map, $enum_map = $null) { |
| 156 |
$changes = [System.Collections.ArrayList]@() |
| 157 |
|
| 158 |
# loop through the passed in map and compare values |
| 159 |
# Name = The name of property in the COM object |
| 160 |
# Value = The new value to compare the existing value with |
| 161 |
foreach ($entry in $map.GetEnumerator()) { |
| 162 |
$new_value = $entry.Value |
| 163 |
|
| 164 |
if ($null -ne $new_value) { |
| 165 |
$property_name = $entry.Name |
| 166 |
$existing_value = $parent_property.$property_name |
| 167 |
if ($existing_value -cne $new_value) { |
| 168 |
try { |
| 169 |
$parent_property.$property_name = $new_value |
| 170 |
} |
| 171 |
catch { |
| 172 |
Fail-Json -obj $result -message "failed to set $property_name property '$property_name' to '$new_value': $($_.Exception.Message)" |
| 173 |
} |
| 174 |
|
| 175 |
if ($null -ne $enum_map -and $enum_map.ContainsKey($property_name)) { |
| 176 |
$enum = [type]$enum_map.$property_name |
| 177 |
$existing_value = [Enum]::ToObject($enum, $existing_value) |
| 178 |
$new_value = [Enum]::ToObject($enum, $new_value) |
| 179 |
} |
| 180 |
[void]$changes.Add("-$property_name=$existing_value`n+$property_name=$new_value") |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return , $changes |
| 186 |
} |
| 187 |
|
| 188 |
Function Set-PropertyForComObject($com_object, $name, $arg, $value) { |
| 189 |
$com_name = Convert-SnakeToPascalCase -snake $arg |
| 190 |
try { |
| 191 |
$com_object.$com_name = $value |
| 192 |
} |
| 193 |
catch { |
| 194 |
Fail-Json -obj $result -message "failed to set $name property '$com_name' to '$value': $($_.Exception.Message)" |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
Function Compare-PropertyList { |
| 199 |
Param( |
| 200 |
$collection, # the collection COM object to manipulate, this must contains the Create method |
| 201 |
[string]$property_name, # human friendly name of the property object, e.g. action/trigger |
| 202 |
[Array]$new, # a list of new properties, passed in by Ansible |
| 203 |
[Array]$existing, # a list of existing properties from the COM object collection |
| 204 |
[Hashtable]$map, # metadata for the collection, see below for the structure |
| 205 |
[string]$enum # the parent enum name for type value |
| 206 |
) |
| 207 |
<## map metadata structure |
| 208 |
{ |
| 209 |
collection type [TASK_ACTION_TYPE] for Actions or [TASK_TRIGGER_TYPE2] for Triggers { |
| 210 |
mandatory = list of mandatory properties for this type, ansible input name not the COM name |
| 211 |
optional = list of optional properties that could be set for this type |
| 212 |
# maps the ansible input object name to the COM name, e.g. working_directory = WorkingDirectory |
| 213 |
map = { |
| 214 |
ansible input name = COM name |
| 215 |
} |
| 216 |
} |
| 217 |
}##> |
| 218 |
# used by both Actions and Triggers to compare the collections of that property |
| 219 |
|
| 220 |
$enum = [type]$enum |
| 221 |
$changes = [System.Collections.ArrayList]@() |
| 222 |
$new_count = $new.Count |
| 223 |
$existing_count = $existing.Count |
| 224 |
|
| 225 |
for ($i = 0; $i -lt $new_count; $i++) { |
| 226 |
if ($i -lt $existing_count) { |
| 227 |
$existing_property = $existing[$i] |
| 228 |
} |
| 229 |
else { |
| 230 |
$existing_property = $null |
| 231 |
} |
| 232 |
$new_property = $new[$i] |
| 233 |
|
| 234 |
# get the type of the property, for action this is set automatically |
| 235 |
if (-not $new_property.ContainsKey("type")) { |
| 236 |
Fail-Json -obj $result -message "entry for $property_name must contain a type key" |
| 237 |
} |
| 238 |
$type = $new_property.type |
| 239 |
$valid_types = $map.Keys |
| 240 |
$property_map = $map.$type |
| 241 |
|
| 242 |
# now let's validate the args for the property |
| 243 |
$mandatory_args = $property_map.mandatory |
| 244 |
$optional_args = $property_map.optional |
| 245 |
$total_args = $mandatory_args + $optional_args |
| 246 |
|
| 247 |
# validate the mandatory arguments |
| 248 |
foreach ($mandatory_arg in $mandatory_args) { |
| 249 |
if (-not $new_property.ContainsKey($mandatory_arg)) { |
| 250 |
$msg = "mandatory key '$mandatory_arg' for $($property_name) is not set, mandatory keys are '$($mandatory_args -join "', '")'" |
| 251 |
Fail-Json -obj $result -message $msg |
| 252 |
} |
| 253 |
} |
| 254 |
# throw a warning if in invalid key was set |
| 255 |
foreach ($entry in $new_property.GetEnumerator()) { |
| 256 |
$key = $entry.Name |
| 257 |
if ($key -notin $total_args -and $key -ne "type") { |
| 258 |
$msg = "key '$key' for $($property_name) entry is not valid and will be ignored, valid keys are '$($total_args -join "', '")'" |
| 259 |
Add-Warning -obj $result -message $msg |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
# now we have validated the input and have gotten the metadata, let's |
| 264 |
# get the diff string |
| 265 |
if ($null -eq $existing_property) { |
| 266 |
# we have more properties than before,just add to the new |
| 267 |
# properties list |
| 268 |
$diff_list = [System.Collections.ArrayList]@() |
| 269 |
|
| 270 |
foreach ($property_arg in $total_args) { |
| 271 |
if ($new_property.ContainsKey($property_arg)) { |
| 272 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 273 |
$property_value = $new_property.$property_arg |
| 274 |
|
| 275 |
if ($property_value -is [Hashtable]) { |
| 276 |
foreach ($kv in $property_value.GetEnumerator()) { |
| 277 |
$sub_com_name = Convert-SnakeToPascalCase -snake $kv.Key |
| 278 |
$sub_property_value = $kv.Value |
| 279 |
[void]$diff_list.Add("+$com_name.$sub_com_name=$sub_property_value") |
| 280 |
} |
| 281 |
} |
| 282 |
else { |
| 283 |
[void]$diff_list.Add("+$com_name=$property_value") |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
[void]$changes.Add("+$property_name[$i] = {`n +Type=$type`n $($diff_list -join ",`n ")`n+}") |
| 289 |
} |
| 290 |
elseif ([Enum]::ToObject($enum, $existing_property.Type) -ne $type) { |
| 291 |
# the types are different so we need to change |
| 292 |
$diff_list = [System.Collections.ArrayList]@() |
| 293 |
|
| 294 |
if ($existing_property.Type -notin $valid_types) { |
| 295 |
[void]$diff_list.Add("-UNKNOWN TYPE $($existing_property.Type)") |
| 296 |
foreach ($property_args in $total_args) { |
| 297 |
if ($new_property.ContainsKey($property_arg)) { |
| 298 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 299 |
$property_value = $new_property.$property_arg |
| 300 |
|
| 301 |
if ($property_value -is [Hashtable]) { |
| 302 |
foreach ($kv in $property_value.GetEnumerator()) { |
| 303 |
$sub_com_name = Convert-SnakeToPascalCase -snake $kv.Key |
| 304 |
$sub_property_value = $kv.Value |
| 305 |
[void]$diff_list.Add("+$com_name.$sub_com_name=$sub_property_value") |
| 306 |
} |
| 307 |
} |
| 308 |
else { |
| 309 |
[void]$diff_list.Add("+$com_name=$property_value") |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
else { |
| 315 |
# we know the types of the existing property |
| 316 |
$existing_type = [Enum]::ToObject([TASK_TRIGGER_TYPE2], $existing_property.Type) |
| 317 |
[void]$diff_list.Add("-Type=$existing_type") |
| 318 |
[void]$diff_list.Add("+Type=$type") |
| 319 |
foreach ($property_arg in $total_args) { |
| 320 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 321 |
$property_value = $new_property.$property_arg |
| 322 |
$existing_value = $existing_property.$com_name |
| 323 |
|
| 324 |
if ($property_value -is [Hashtable]) { |
| 325 |
foreach ($kv in $property_value.GetEnumerator()) { |
| 326 |
$sub_property_value = $kv.Value |
| 327 |
$sub_com_name = Convert-SnakeToPascalCase -snake $kv.Key |
| 328 |
$sub_existing_value = $existing_property.$com_name.$sub_com_name |
| 329 |
|
| 330 |
if ($null -ne $sub_property_value) { |
| 331 |
[void]$diff_list.Add("+$com_name.$sub_com_name=$sub_property_value") |
| 332 |
} |
| 333 |
|
| 334 |
if ($null -ne $sub_existing_value) { |
| 335 |
[void]$diff_list.Add("-$com_name.$sub_com_name=$sub_existing_value") |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
else { |
| 340 |
if ($null -ne $property_value) { |
| 341 |
[void]$diff_list.Add("+$com_name=$property_value") |
| 342 |
} |
| 343 |
|
| 344 |
if ($null -ne $existing_value) { |
| 345 |
[void]$diff_list.Add("-$com_name=$existing_value") |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
[void]$changes.Add("$property_name[$i] = {`n $($diff_list -join ",`n ")`n}") |
| 352 |
} |
| 353 |
else { |
| 354 |
# compare the properties of existing and new |
| 355 |
$diff_list = [System.Collections.ArrayList]@() |
| 356 |
|
| 357 |
foreach ($property_arg in $total_args) { |
| 358 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 359 |
$property_value = $new_property.$property_arg |
| 360 |
$existing_value = $existing_property.$com_name |
| 361 |
|
| 362 |
if ($property_value -is [Hashtable]) { |
| 363 |
foreach ($kv in $property_value.GetEnumerator()) { |
| 364 |
$sub_property_value = $kv.Value |
| 365 |
|
| 366 |
if ($null -ne $sub_property_value) { |
| 367 |
$sub_com_name = Convert-SnakeToPascalCase -snake $kv.Key |
| 368 |
$sub_existing_value = $existing_property.$com_name.$sub_com_name |
| 369 |
|
| 370 |
if ($sub_property_value -cne $sub_existing_value) { |
| 371 |
[void]$diff_list.Add("-$com_name.$sub_com_name=$sub_existing_value") |
| 372 |
[void]$diff_list.Add("+$com_name.$sub_com_name=$sub_property_value") |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
elseif ($null -ne $property_value -and $property_value -cne $existing_value) { |
| 378 |
[void]$diff_list.Add("-$com_name=$existing_value") |
| 379 |
[void]$diff_list.Add("+$com_name=$property_value") |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if ($diff_list.Count -gt 0) { |
| 384 |
[void]$changes.Add("$property_name[$i] = {`n $($diff_list -join ",`n ")`n}") |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
# finally rebuild the new property collection |
| 389 |
$new_object = $collection.Create($type) |
| 390 |
foreach ($property_arg in $total_args) { |
| 391 |
$new_value = $new_property.$property_arg |
| 392 |
if ($new_value -is [Hashtable]) { |
| 393 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 394 |
$new_object_property = $new_object.$com_name |
| 395 |
|
| 396 |
foreach ($kv in $new_value.GetEnumerator()) { |
| 397 |
$value = $kv.Value |
| 398 |
if ($null -ne $value) { |
| 399 |
Set-PropertyForComObject -com_object $new_object_property -name $property_name -arg $kv.Key -value $value |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
elseif ($null -ne $new_value) { |
| 404 |
Set-PropertyForComObject -com_object $new_object -name $property_name -arg $property_arg -value $new_value |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
# if there were any extra properties not in the new list, create diff str |
| 410 |
if ($existing_count -gt $new_count) { |
| 411 |
for ($i = $new_count; $i -lt $existing_count; $i++) { |
| 412 |
$diff_list = [System.Collections.ArrayList]@() |
| 413 |
$existing_property = $existing[$i] |
| 414 |
$existing_type = [Enum]::ToObject($enum, $existing_property.Type) |
| 415 |
|
| 416 |
if ($map.ContainsKey($existing_type)) { |
| 417 |
$property_map = $map.$existing_type |
| 418 |
$property_args = $property_map.mandatory + $property_map.optional |
| 419 |
|
| 420 |
foreach ($property_arg in $property_args) { |
| 421 |
$com_name = Convert-SnakeToPascalCase -snake $property_arg |
| 422 |
$existing_value = $existing_property.$com_name |
| 423 |
if ($null -ne $existing_value) { |
| 424 |
[void]$diff_list.Add("-$com_name=$existing_value") |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
else { |
| 429 |
[void]$diff_list.Add("-UNKNOWN TYPE $existing_type") |
| 430 |
} |
| 431 |
|
| 432 |
[void]$changes.Add("-$property_name[$i] = {`n $($diff_list -join ",`n ")`n-}") |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return , $changes |
| 437 |
} |
| 438 |
|
| 439 |
Function Compare-Action($task_definition) { |
| 440 |
# compares the Actions property and returns a list of list of changed |
| 441 |
# actions for use in a diff string |
| 442 |
# ActionCollection - https://msdn.microsoft.com/en-us/library/windows/desktop/aa446804(v=vs.85).aspx |
| 443 |
# Action - https://msdn.microsoft.com/en-us/library/windows/desktop/aa446803(v=vs.85).aspx |
| 444 |
if ($null -eq $actions) { |
| 445 |
return , [System.Collections.ArrayList]@() |
| 446 |
} |
| 447 |
|
| 448 |
$task_actions = $task_definition.Actions |
| 449 |
$existing_count = $task_actions.Count |
| 450 |
|
| 451 |
# because we clear the actions and re-add them to keep the order, we need |
| 452 |
# to convert the existing actions to a new list. |
| 453 |
# The Item property in actions starts at 1 |
| 454 |
$existing_actions = [System.Collections.ArrayList]@() |
| 455 |
for ($i = 1; $i -le $existing_count; $i++) { |
| 456 |
[void]$existing_actions.Add($task_actions.Item($i)) |
| 457 |
} |
| 458 |
if ($existing_count -gt 0) { |
| 459 |
$task_actions.Clear() |
| 460 |
} |
| 461 |
|
| 462 |
$map = @{ |
| 463 |
[TASK_ACTION_TYPE]::TASK_ACTION_EXEC = @{ |
| 464 |
mandatory = @('path') |
| 465 |
optional = @('arguments', 'working_directory') |
| 466 |
} |
| 467 |
} |
| 468 |
$changes = Compare-PropertyList -collection $task_actions -property_name "action" -new $actions -existing $existing_actions -map $map -enum TASK_ACTION_TYPE |
| 469 |
|
| 470 |
return , $changes |
| 471 |
} |
| 472 |
|
| 473 |
Function Compare-Principal($task_definition, $task_definition_xml) { |
| 474 |
# compares the Principal property and returns a list of changed objects for |
| 475 |
# use in a diff string |
| 476 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382071(v=vs.85).aspx |
| 477 |
$principal_map = @{ |
| 478 |
DisplayName = $display_name |
| 479 |
LogonType = $logon_type |
| 480 |
RunLevel = $run_level |
| 481 |
} |
| 482 |
$enum_map = @{ |
| 483 |
LogonType = "TASK_LOGON_TYPE" |
| 484 |
RunLevel = "TASK_RUN_LEVEL" |
| 485 |
} |
| 486 |
$task_principal = $task_definition.Principal |
| 487 |
$changes = Compare-Property -property_name "Principal" -parent_property $task_principal -map $principal_map -enum_map $enum_map |
| 488 |
|
| 489 |
# Principal.UserId and GroupId only returns the username portion of the |
| 490 |
# username, skipping the domain or server name. This makes the |
| 491 |
# comparison process useless so we need to parse the task XML to get |
| 492 |
# the actual sid/username. Depending on OS version this could be the SID |
| 493 |
# or it could be the username, we need to handle that accordingly |
| 494 |
$principal_username_sid = $task_definition_xml.Task.Principals.Principal.UserId |
| 495 |
if ($null -ne $principal_username_sid -and $principal_username_sid -notmatch "^S-\d-\d+(-\d+){1,14}(-\d+){0,1}$") { |
| 496 |
$principal_username_sid = Convert-ToSID -account_name $principal_username_sid |
| 497 |
} |
| 498 |
$principal_group_sid = $task_definition_xml.Task.Principals.Principal.GroupId |
| 499 |
if ($null -ne $principal_group_sid -and $principal_group_sid -notmatch "^S-\d-\d+(-\d+){1,14}(-\d+){0,1}$") { |
| 500 |
$principal_group_sid = Convert-ToSID -account_name $principal_group_sid |
| 501 |
} |
| 502 |
|
| 503 |
if ($null -ne $username_sid) { |
| 504 |
$new_user_name = Convert-FromSid -sid $username_sid |
| 505 |
if ($null -ne $principal_group_sid) { |
| 506 |
$existing_account_name = Convert-FromSid -sid $principal_group_sid |
| 507 |
[void]$changes.Add("-GroupId=$existing_account_name`n+UserId=$new_user_name") |
| 508 |
$task_principal.UserId = $new_user_name |
| 509 |
$task_principal.GroupId = $null |
| 510 |
} |
| 511 |
elseif ($null -eq $principal_username_sid) { |
| 512 |
[void]$changes.Add("+UserId=$new_user_name") |
| 513 |
$task_principal.UserId = $new_user_name |
| 514 |
} |
| 515 |
elseif ($principal_username_sid -ne $username_sid) { |
| 516 |
$existing_account_name = Convert-FromSid -sid $principal_username_sid |
| 517 |
[void]$changes.Add("-UserId=$existing_account_name`n+UserId=$new_user_name") |
| 518 |
$task_principal.UserId = $new_user_name |
| 519 |
} |
| 520 |
} |
| 521 |
if ($null -ne $group_sid) { |
| 522 |
$new_group_name = Convert-FromSid -sid $group_sid |
| 523 |
if ($null -ne $principal_username_sid) { |
| 524 |
$existing_account_name = Convert-FromSid -sid $principal_username_sid |
| 525 |
[void]$changes.Add("-UserId=$existing_account_name`n+GroupId=$new_group_name") |
| 526 |
$task_principal.UserId = $null |
| 527 |
$task_principal.GroupId = $new_group_name |
| 528 |
} |
| 529 |
elseif ($null -eq $principal_group_sid) { |
| 530 |
[void]$changes.Add("+GroupId=$new_group_name") |
| 531 |
$task_principal.GroupId = $new_group_name |
| 532 |
} |
| 533 |
elseif ($principal_group_sid -ne $group_sid) { |
| 534 |
$existing_account_name = Convert-FromSid -sid $principal_group_sid |
| 535 |
[void]$changes.Add("-GroupId=$existing_account_name`n+GroupId=$new_group_name") |
| 536 |
$task_principal.GroupId = $new_group_name |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
return , $changes |
| 541 |
} |
| 542 |
|
| 543 |
Function Compare-RegistrationInfo($task_definition) { |
| 544 |
# compares the RegistrationInfo property and returns a list of changed |
| 545 |
# objects for use in a diff string |
| 546 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382100(v=vs.85).aspx |
| 547 |
$reg_info_map = @{ |
| 548 |
Author = $author |
| 549 |
Date = $date |
| 550 |
Description = $description |
| 551 |
Source = $source |
| 552 |
Version = $version |
| 553 |
} |
| 554 |
$changes = Compare-Property -property_name "RegistrationInfo" -parent_property $task_definition.RegistrationInfo -map $reg_info_map |
| 555 |
|
| 556 |
return , $changes |
| 557 |
} |
| 558 |
|
| 559 |
Function Compare-Setting($task_definition) { |
| 560 |
# compares the task Settings property and returns a list of changed objects |
| 561 |
# for use in a diff string |
| 562 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383480(v=vs.85).aspx |
| 563 |
$settings_map = @{ |
| 564 |
AllowDemandStart = $allow_demand_start |
| 565 |
AllowHardTerminate = $allow_hard_terminate |
| 566 |
Compatibility = $compatibility |
| 567 |
DeleteExpiredTaskAfter = $delete_expired_task_after |
| 568 |
DisallowStartIfOnBatteries = $disallow_start_if_on_batteries |
| 569 |
ExecutionTimeLimit = $execution_time_limit |
| 570 |
Enabled = $enabled |
| 571 |
Hidden = $hidden |
| 572 |
# IdleSettings = $idle_settings # TODO: this takes in a COM object |
| 573 |
MultipleInstances = $multiple_instances |
| 574 |
# NetworkSettings = $network_settings # TODO: this takes in a COM object |
| 575 |
Priority = $priority |
| 576 |
RestartCount = $restart_count |
| 577 |
RestartInterval = $restart_interval |
| 578 |
RunOnlyIfIdle = $run_only_if_idle |
| 579 |
RunOnlyIfNetworkAvailable = $run_only_if_network_available |
| 580 |
StartWhenAvailable = $start_when_available |
| 581 |
StopIfGoingOnBatteries = $stop_if_going_on_batteries |
| 582 |
WakeToRun = $wake_to_run |
| 583 |
} |
| 584 |
$changes = Compare-Property -property_name "Settings" -parent_property $task_definition.Settings -map $settings_map |
| 585 |
|
| 586 |
return , $changes |
| 587 |
} |
| 588 |
|
| 589 |
Function Compare-Trigger($task_definition) { |
| 590 |
# compares the task Triggers property and returns a list of changed objects |
| 591 |
# for use in a diff string |
| 592 |
# TriggerCollection - https://msdn.microsoft.com/en-us/library/windows/desktop/aa383875(v=vs.85).aspx |
| 593 |
# Trigger - https://msdn.microsoft.com/en-us/library/windows/desktop/aa383868(v=vs.85).aspx |
| 594 |
if ($null -eq $triggers) { |
| 595 |
return , [System.Collections.ArrayList]@() |
| 596 |
} |
| 597 |
|
| 598 |
$task_triggers = $task_definition.Triggers |
| 599 |
$existing_count = $task_triggers.Count |
| 600 |
|
| 601 |
# because we clear the actions and re-add them to keep the order, we need |
| 602 |
# to convert the existing actions to a new list. |
| 603 |
# The Item property in actions starts at 1 |
| 604 |
$existing_triggers = [System.Collections.ArrayList]@() |
| 605 |
for ($i = 1; $i -le $existing_count; $i++) { |
| 606 |
[void]$existing_triggers.Add($task_triggers.Item($i)) |
| 607 |
} |
| 608 |
if ($existing_count -gt 0) { |
| 609 |
$task_triggers.Clear() |
| 610 |
} |
| 611 |
|
| 612 |
$map = @{ |
| 613 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_BOOT = @{ |
| 614 |
mandatory = @() |
| 615 |
optional = @('delay', 'enabled', 'end_boundary', 'execution_time_limit', 'start_boundary', 'repetition') |
| 616 |
} |
| 617 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_DAILY = @{ |
| 618 |
mandatory = @('start_boundary') |
| 619 |
optional = @('days_interval', 'enabled', 'end_boundary', 'execution_time_limit', 'random_delay', 'repetition') |
| 620 |
} |
| 621 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_EVENT = @{ |
| 622 |
mandatory = @('subscription') |
| 623 |
# TODO: ValueQueries is a COM object |
| 624 |
optional = @('delay', 'enabled', 'end_boundary', 'execution_time_limit', 'start_boundary', 'repetition') |
| 625 |
} |
| 626 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_IDLE = @{ |
| 627 |
mandatory = @() |
| 628 |
optional = @('enabled', 'end_boundary', 'execution_time_limit', 'start_boundary', 'repetition') |
| 629 |
} |
| 630 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_LOGON = @{ |
| 631 |
mandatory = @() |
| 632 |
optional = @('delay', 'enabled', 'end_boundary', 'execution_time_limit', 'start_boundary', 'user_id', 'repetition') |
| 633 |
} |
| 634 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_MONTHLYDOW = @{ |
| 635 |
mandatory = @('start_boundary') |
| 636 |
# Make sure run_on_last_week_of_month comes after weeks_of_month |
| 637 |
# https://github.com/ansible-collections/community.windows/issues/414 |
| 638 |
optional = @('days_of_week', 'enabled', 'end_boundary', 'execution_time_limit', 'months_of_year', 'random_delay', 'weeks_of_month', |
| 639 |
'run_on_last_week_of_month', 'repetition') |
| 640 |
} |
| 641 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_MONTHLY = @{ |
| 642 |
mandatory = @('days_of_month', 'start_boundary') |
| 643 |
optional = @('enabled', 'end_boundary', 'execution_time_limit', 'months_of_year', 'random_delay', 'run_on_last_day_of_month', |
| 644 |
'start_boundary', 'repetition') |
| 645 |
} |
| 646 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_REGISTRATION = @{ |
| 647 |
mandatory = @() |
| 648 |
optional = @('delay', 'enabled', 'end_boundary', 'execution_time_limit', 'start_boundary', 'repetition') |
| 649 |
} |
| 650 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_TIME = @{ |
| 651 |
mandatory = @('start_boundary') |
| 652 |
optional = @('enabled', 'end_boundary', 'execution_time_limit', 'random_delay', 'repetition') |
| 653 |
} |
| 654 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_WEEKLY = @{ |
| 655 |
mandatory = @('days_of_week', 'start_boundary') |
| 656 |
optional = @('enabled', 'end_boundary', 'execution_time_limit', 'random_delay', 'weeks_interval', 'repetition') |
| 657 |
} |
| 658 |
[TASK_TRIGGER_TYPE2]::TASK_TRIGGER_SESSION_STATE_CHANGE = @{ |
| 659 |
mandatory = @() |
| 660 |
optional = @('delay', 'enabled', 'end_boundary', 'execution_time_limit', 'repetition', 'start_boundary', 'state_change', 'user_id' ) |
| 661 |
} |
| 662 |
} |
| 663 |
$compareParams = @{ |
| 664 |
collection = $task_triggers |
| 665 |
property_name = "trigger" |
| 666 |
new = $triggers |
| 667 |
existing = $existing_triggers |
| 668 |
map = $map |
| 669 |
enum = "TASK_TRIGGER_TYPE2" |
| 670 |
} |
| 671 |
$changes = Compare-PropertyList @compareParams |
| 672 |
|
| 673 |
return , $changes |
| 674 |
} |
| 675 |
|
| 676 |
Function Test-TaskExist($task_folder, $name) { |
| 677 |
# checks if a task exists in the TaskFolder COM object, returns null if the |
| 678 |
# task does not exist, otherwise returns the RegisteredTask object |
| 679 |
$task = $null |
| 680 |
if ($task_folder) { |
| 681 |
$raw_tasks = $task_folder.GetTasks(1) # 1 = TASK_ENUM_HIDDEN |
| 682 |
|
| 683 |
for ($i = 1; $i -le $raw_tasks.Count; $i++) { |
| 684 |
if ($raw_tasks.Item($i).Name -eq $name) { |
| 685 |
$task = $raw_tasks.Item($i) |
| 686 |
break |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
return $task |
| 692 |
} |
| 693 |
|
| 694 |
Function Test-XmlDurationFormat($key, $value) { |
| 695 |
# validate value is in the Duration Data Type format |
| 696 |
# PnYnMnDTnHnMnS |
| 697 |
try { |
| 698 |
$time_span = [System.Xml.XmlConvert]::ToTimeSpan($value) |
| 699 |
return $time_span |
| 700 |
} |
| 701 |
catch [System.FormatException] { |
| 702 |
Fail-Json -obj $result -message "trigger option '$key' must be in the XML duration format but was '$value'" |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
###################################### |
| 707 |
### VALIDATION/BUILDING OF OPTIONS ### |
| 708 |
###################################### |
| 709 |
|
| 710 |
# invalid characters in task name |
| 711 |
$invalid_name_chars = '\/:*?"<>|' |
| 712 |
$invalid_name_chars_regex = "[$([regex]::Escape($invalid_name_chars))]" |
| 713 |
|
| 714 |
if ($name -cmatch $invalid_name_chars_regex) { |
| 715 |
Fail-Json -obj $result -message "Invalid task name '$name'. The following characters are not valid: $invalid_name_chars" |
| 716 |
} |
| 717 |
|
| 718 |
# convert username and group to SID if set |
| 719 |
$username_sid = $null |
| 720 |
if ($username) { |
| 721 |
$username_sid = Convert-ToSID -account_name $username |
| 722 |
} |
| 723 |
$group_sid = $null |
| 724 |
if ($group) { |
| 725 |
$group_sid = Convert-ToSID -account_name $group |
| 726 |
} |
| 727 |
|
| 728 |
# validate store_password and logon_type |
| 729 |
if ($null -ne $logon_type) { |
| 730 |
$full_enum_name = "TASK_LOGON_$($logon_type.ToUpper())" |
| 731 |
$logon_type = [TASK_LOGON_TYPE]::$full_enum_name |
| 732 |
} |
| 733 |
|
| 734 |
# now validate the logon_type option with the other parameters |
| 735 |
if ($null -ne $username -and $null -ne $group) { |
| 736 |
Fail-Json -obj $result -message "username and group can not be set at the same time" |
| 737 |
} |
| 738 |
if ($null -ne $logon_type) { |
| 739 |
if ($logon_type -eq [TASK_LOGON_TYPE]::TASK_LOGON_S4U -and $null -eq $password) { |
| 740 |
Fail-Json -obj $result -message "password must be set when logon_type=s4u" |
| 741 |
} |
| 742 |
|
| 743 |
if ($logon_type -eq [TASK_LOGON_TYPE]::TASK_LOGON_GROUP -and $null -eq $group) { |
| 744 |
Fail-Json -obj $result -message "group must be set when logon_type=group" |
| 745 |
} |
| 746 |
|
| 747 |
# SIDs == Local System, Local Service and Network Service |
| 748 |
if ($logon_type -eq [TASK_LOGON_TYPE]::TASK_LOGON_SERVICE_ACCOUNT -and $username_sid -notin @("S-1-5-18", "S-1-5-19", "S-1-5-20")) { |
| 749 |
Fail-Json -obj $result -message "username must be SYSTEM, LOCAL SERVICE or NETWORK SERVICE when logon_type=service_account" |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
# convert the run_level to enum value |
| 754 |
if ($null -ne $run_level) { |
| 755 |
if ($run_level -eq "limited") { |
| 756 |
$run_level = [TASK_RUN_LEVEL]::TASK_RUNLEVEL_LUA |
| 757 |
} |
| 758 |
else { |
| 759 |
$run_level = [TASK_RUN_LEVEL]::TASK_RUNLEVEL_HIGHEST |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
# manually add the only support action type for each action - also convert PSCustomObject to Hashtable |
| 764 |
for ($i = 0; $i -lt $actions.Count; $i++) { |
| 765 |
$action = $actions[$i] |
| 766 |
$action.type = [TASK_ACTION_TYPE]::TASK_ACTION_EXEC |
| 767 |
if (-not $action.ContainsKey("path")) { |
| 768 |
Fail-Json -obj $result -message "action entry must contain the key 'path'" |
| 769 |
} |
| 770 |
$actions[$i] = $action |
| 771 |
} |
| 772 |
|
| 773 |
# convert and validate the triggers - and convert PSCustomObject to Hashtable |
| 774 |
for ($i = 0; $i -lt $triggers.Count; $i++) { |
| 775 |
$trigger = $triggers[$i] |
| 776 |
$valid_trigger_types = @('event', 'time', 'daily', 'weekly', 'monthly', 'monthlydow', 'idle', 'registration', 'boot', 'logon', 'session_state_change') |
| 777 |
if (-not $trigger.ContainsKey("type")) { |
| 778 |
Fail-Json -obj $result -message "a trigger entry must contain a key 'type' with a value of '$($valid_trigger_types -join "', '")'" |
| 779 |
} |
| 780 |
|
| 781 |
$trigger_type = $trigger.type |
| 782 |
if ($trigger_type -notin $valid_trigger_types) { |
| 783 |
$msg = "the specified trigger type '$trigger_type' is not valid, type must be a value of '$($valid_trigger_types -join "', '")'" |
| 784 |
Fail-Json -obj $result -message $msg |
| 785 |
} |
| 786 |
|
| 787 |
$full_enum_name = "TASK_TRIGGER_$($trigger_type.ToUpper())" |
| 788 |
$trigger_type = [TASK_TRIGGER_TYPE2]::$full_enum_name |
| 789 |
$trigger.type = $trigger_type |
| 790 |
|
| 791 |
$date_properties = @('start_boundary', 'end_boundary') |
| 792 |
foreach ($property_name in $date_properties) { |
| 793 |
# validate the date is in the DateTime format |
| 794 |
# yyyy-mm-ddThh:mm:ss |
| 795 |
if ($trigger.ContainsKey($property_name)) { |
| 796 |
$date_value = $trigger.$property_name |
| 797 |
try { |
| 798 |
$date = Get-Date -Date $date_value -Format "yyyy-MM-dd'T'HH:mm:ssK" |
| 799 |
# make sure we convert it to the full string format |
| 800 |
$trigger.$property_name = $date.ToString() |
| 801 |
} |
| 802 |
catch [System.Management.Automation.ParameterBindingException] { |
| 803 |
Fail-Json -obj $result -message "trigger option '$property_name' must be in the format 'YYYY-MM-DDThh:mm:ss' format but was '$date_value'" |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
$time_properties = @('execution_time_limit', 'delay', 'random_delay') |
| 809 |
foreach ($property_name in $time_properties) { |
| 810 |
if ($trigger.ContainsKey($property_name)) { |
| 811 |
$time_span = $trigger.$property_name |
| 812 |
Test-XmlDurationFormat -key $property_name -value $time_span |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
if ($trigger.ContainsKey("repetition")) { |
| 817 |
$interval_timespan = $null |
| 818 |
if ($trigger.repetition.ContainsKey("interval") -and $null -ne $trigger.repetition.interval) { |
| 819 |
$interval_timespan = Test-XmlDurationFormat -key "interval" -value $trigger.repetition.interval |
| 820 |
} |
| 821 |
|
| 822 |
$duration_timespan = $null |
| 823 |
if ($trigger.repetition.ContainsKey("duration") -and $null -ne $trigger.repetition.duration) { |
| 824 |
$duration_timespan = Test-XmlDurationFormat -key "duration" -value $trigger.repetition.duration |
| 825 |
} |
| 826 |
|
| 827 |
if ($null -ne $interval_timespan -and $null -ne $duration_timespan -and $interval_timespan -gt $duration_timespan) { |
| 828 |
$msg = -join @( |
| 829 |
"trigger repetition option 'interval' value '$($trigger.repetition.interval)' " |
| 830 |
"must be less than or equal to 'duration' value '$($trigger.repetition.duration)'" |
| 831 |
) |
| 832 |
Fail-Json -obj $result -message $msg |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
# convert out human readble text to the hex values for these properties |
| 837 |
if ($trigger.ContainsKey("days_of_week")) { |
| 838 |
$days = $trigger.days_of_week |
| 839 |
if ($days -is [String]) { |
| 840 |
$days = $days.Split(",").Trim() |
| 841 |
} |
| 842 |
elseif ($days -isnot [Array]) { |
| 843 |
$days = @($days) |
| 844 |
} |
| 845 |
|
| 846 |
$day_value = 0 |
| 847 |
foreach ($day in $days) { |
| 848 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382057(v=vs.85).aspx |
| 849 |
switch ($day) { |
| 850 |
sunday { $day_value = $day_value -bor 0x01 } |
| 851 |
monday { $day_value = $day_value -bor 0x02 } |
| 852 |
tuesday { $day_value = $day_value -bor 0x04 } |
| 853 |
wednesday { $day_value = $day_value -bor 0x08 } |
| 854 |
thursday { $day_value = $day_value -bor 0x10 } |
| 855 |
friday { $day_value = $day_value -bor 0x20 } |
| 856 |
saturday { $day_value = $day_value -bor 0x40 } |
| 857 |
default { Fail-Json -obj $result -message "invalid day of week '$day', check the spelling matches the full day name" } |
| 858 |
} |
| 859 |
} |
| 860 |
if ($day_value -eq 0) { |
| 861 |
$day_value = $null |
| 862 |
} |
| 863 |
|
| 864 |
$trigger.days_of_week = $day_value |
| 865 |
} |
| 866 |
if ($trigger.ContainsKey("days_of_month")) { |
| 867 |
$days = $trigger.days_of_month |
| 868 |
if ($days -is [String]) { |
| 869 |
$days = $days.Split(",").Trim() |
| 870 |
} |
| 871 |
elseif ($days -isnot [Array]) { |
| 872 |
$days = @($days) |
| 873 |
} |
| 874 |
|
| 875 |
$day_value = 0 |
| 876 |
foreach ($day in $days) { |
| 877 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382063(v=vs.85).aspx |
| 878 |
switch ($day) { |
| 879 |
1 { $day_value = $day_value -bor 0x01 } |
| 880 |
2 { $day_value = $day_value -bor 0x02 } |
| 881 |
3 { $day_value = $day_value -bor 0x04 } |
| 882 |
4 { $day_value = $day_value -bor 0x08 } |
| 883 |
5 { $day_value = $day_value -bor 0x10 } |
| 884 |
6 { $day_value = $day_value -bor 0x20 } |
| 885 |
7 { $day_value = $day_value -bor 0x40 } |
| 886 |
8 { $day_value = $day_value -bor 0x80 } |
| 887 |
9 { $day_value = $day_value -bor 0x100 } |
| 888 |
10 { $day_value = $day_value -bor 0x200 } |
| 889 |
11 { $day_value = $day_value -bor 0x400 } |
| 890 |
12 { $day_value = $day_value -bor 0x800 } |
| 891 |
13 { $day_value = $day_value -bor 0x1000 } |
| 892 |
14 { $day_value = $day_value -bor 0x2000 } |
| 893 |
15 { $day_value = $day_value -bor 0x4000 } |
| 894 |
16 { $day_value = $day_value -bor 0x8000 } |
| 895 |
17 { $day_value = $day_value -bor 0x10000 } |
| 896 |
18 { $day_value = $day_value -bor 0x20000 } |
| 897 |
19 { $day_value = $day_value -bor 0x40000 } |
| 898 |
20 { $day_value = $day_value -bor 0x80000 } |
| 899 |
21 { $day_value = $day_value -bor 0x100000 } |
| 900 |
22 { $day_value = $day_value -bor 0x200000 } |
| 901 |
23 { $day_value = $day_value -bor 0x400000 } |
| 902 |
24 { $day_value = $day_value -bor 0x800000 } |
| 903 |
25 { $day_value = $day_value -bor 0x1000000 } |
| 904 |
26 { $day_value = $day_value -bor 0x2000000 } |
| 905 |
27 { $day_value = $day_value -bor 0x4000000 } |
| 906 |
28 { $day_value = $day_value -bor 0x8000000 } |
| 907 |
29 { $day_value = $day_value -bor 0x10000000 } |
| 908 |
30 { $day_value = $day_value -bor 0x20000000 } |
| 909 |
31 { $day_value = $day_value -bor 0x40000000 } |
| 910 |
default { Fail-Json -obj $result -message "invalid day of month '$day', please specify numbers from 1-31" } |
| 911 |
} |
| 912 |
} |
| 913 |
if ($day_value -eq 0) { |
| 914 |
$day_value = $null |
| 915 |
} |
| 916 |
$trigger.days_of_month = $day_value |
| 917 |
} |
| 918 |
if ($trigger.ContainsKey("weeks_of_month")) { |
| 919 |
$weeks = $trigger.weeks_of_month |
| 920 |
if ($weeks -is [String]) { |
| 921 |
$weeks = $weeks.Split(",").Trim() |
| 922 |
} |
| 923 |
elseif ($weeks -isnot [Array]) { |
| 924 |
$weeks = @($weeks) |
| 925 |
} |
| 926 |
|
| 927 |
$week_value = 0 |
| 928 |
foreach ($week in $weeks) { |
| 929 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382061(v=vs.85).aspx |
| 930 |
switch ($week) { |
| 931 |
1 { $week_value = $week_value -bor 0x01 } |
| 932 |
2 { $week_value = $week_value -bor 0x02 } |
| 933 |
3 { $week_value = $week_value -bor 0x04 } |
| 934 |
4 { $week_value = $week_value -bor 0x08 } |
| 935 |
default { Fail-Json -obj $result -message "invalid week of month '$week', please specify weeks from 1-4" } |
| 936 |
} |
| 937 |
|
| 938 |
} |
| 939 |
if ($week_value -eq 0) { |
| 940 |
$week_value = $null |
| 941 |
} |
| 942 |
$trigger.weeks_of_month = $week_value |
| 943 |
} |
| 944 |
if ($trigger.ContainsKey("months_of_year")) { |
| 945 |
$months = $trigger.months_of_year |
| 946 |
if ($months -is [String]) { |
| 947 |
$months = $months.Split(",").Trim() |
| 948 |
} |
| 949 |
elseif ($months -isnot [Array]) { |
| 950 |
$months = @($months) |
| 951 |
} |
| 952 |
|
| 953 |
$month_value = 0 |
| 954 |
foreach ($month in $months) { |
| 955 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382064(v=vs.85).aspx |
| 956 |
switch ($month) { |
| 957 |
january { $month_value = $month_value -bor 0x01 } |
| 958 |
february { $month_value = $month_value -bor 0x02 } |
| 959 |
march { $month_value = $month_value -bor 0x04 } |
| 960 |
april { $month_value = $month_value -bor 0x08 } |
| 961 |
may { $month_value = $month_value -bor 0x10 } |
| 962 |
june { $month_value = $month_value -bor 0x20 } |
| 963 |
july { $month_value = $month_value -bor 0x40 } |
| 964 |
august { $month_value = $month_value -bor 0x80 } |
| 965 |
september { $month_value = $month_value -bor 0x100 } |
| 966 |
october { $month_value = $month_value -bor 0x200 } |
| 967 |
november { $month_value = $month_value -bor 0x400 } |
| 968 |
december { $month_value = $month_value -bor 0x800 } |
| 969 |
default { Fail-Json -obj $result -message "invalid month name '$month', please specify full month name" } |
| 970 |
} |
| 971 |
} |
| 972 |
if ($month_value -eq 0) { |
| 973 |
$month_value = $null |
| 974 |
} |
| 975 |
$trigger.months_of_year = $month_value |
| 976 |
} |
| 977 |
if ($trigger.ContainsKey("state_change")) { |
| 978 |
$trigger.state_change = switch ($trigger.state_change) { |
| 979 |
console_connect { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_CONSOLE_CONNECT } |
| 980 |
console_disconnect { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_CONSOLE_DISCONNECT } |
| 981 |
remote_connect { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_REMOTE_CONNECT } |
| 982 |
remote_disconnect { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_REMOTE_DISCONNECT } |
| 983 |
session_lock { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_SESSION_LOCK } |
| 984 |
session_unlock { [TASK_SESSION_STATE_CHANGE_TYPE]::TASK_SESSION_UNLOCK } |
| 985 |
default { |
| 986 |
Fail-Json -obj $result -message "invalid state_change '$($trigger.state_change)'" |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
$triggers[$i] = $trigger |
| 991 |
} |
| 992 |
|
| 993 |
# add \ to start of path if it is not already there |
| 994 |
if (-not $path.StartsWith("\")) { |
| 995 |
$path = "\$path" |
| 996 |
} |
| 997 |
# ensure path does not end with \ if more than 1 char |
| 998 |
if ($path.EndsWith("\") -and $path.Length -ne 1) { |
| 999 |
$path = $path.Substring(0, $path.Length - 1) |
| 1000 |
} |
| 1001 |
|
| 1002 |
######################## |
| 1003 |
### START CODE BLOCK ### |
| 1004 |
######################## |
| 1005 |
$service = New-Object -ComObject Schedule.Service |
| 1006 |
try { |
| 1007 |
$service.Connect() |
| 1008 |
} |
| 1009 |
catch { |
| 1010 |
Fail-Json -obj $result -message "failed to connect to the task scheduler service: $($_.Exception.Message)" |
| 1011 |
} |
| 1012 |
|
| 1013 |
# check that the path for the task set exists, create if need be |
| 1014 |
try { |
| 1015 |
$task_folder = $service.GetFolder($path) |
| 1016 |
} |
| 1017 |
catch { |
| 1018 |
$task_folder = $null |
| 1019 |
} |
| 1020 |
|
| 1021 |
# try and get the task at the path |
| 1022 |
$task = Test-TaskExist -task_folder $task_folder -name $name |
| 1023 |
$task_path = Join-Path -Path $path -ChildPath $name |
| 1024 |
|
| 1025 |
if ($state -eq "absent") { |
| 1026 |
if ($null -ne $task) { |
| 1027 |
if (-not $check_mode) { |
| 1028 |
try { |
| 1029 |
$task_folder.DeleteTask($name, 0) |
| 1030 |
} |
| 1031 |
catch { |
| 1032 |
Fail-Json -obj $result -message "failed to delete task '$name' at path '$path': $($_.Exception.Message)" |
| 1033 |
} |
| 1034 |
} |
| 1035 |
if ($diff_mode) { |
| 1036 |
$result.diff.prepared = "-[Task]`n-$task_path`n" |
| 1037 |
} |
| 1038 |
$result.changed = $true |
| 1039 |
|
| 1040 |
# check if current folder has any more tasks |
| 1041 |
$other_tasks = $task_folder.GetTasks(1) # 1 = TASK_ENUM_HIDDEN |
| 1042 |
if ($other_tasks.Count -eq 0 -and $task_folder.Name -ne "\") { |
| 1043 |
try { |
| 1044 |
$task_folder.DeleteFolder($null, $null) |
| 1045 |
} |
| 1046 |
catch { |
| 1047 |
Fail-Json -obj $result -message "failed to delete empty task folder '$path' after task deletion: $($_.Exception.Message)" |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
else { |
| 1053 |
if ($null -eq $task) { |
| 1054 |
$create_diff_string = "+[Task]`n+$task_path`n`n" |
| 1055 |
# to create a bare minimum task we need 1 action |
| 1056 |
if ($null -eq $actions -or $actions.Count -eq 0) { |
| 1057 |
Fail-Json -obj $result -message "cannot create a task with no actions, set at least one action with a path to an executable" |
| 1058 |
} |
| 1059 |
|
| 1060 |
# Create a bare minimum task here, further properties will be set later on |
| 1061 |
$task_definition = $service.NewTask(0) |
| 1062 |
|
| 1063 |
# Set Actions info |
| 1064 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa446803(v=vs.85).aspx |
| 1065 |
$create_diff_string += "[Actions]`n" |
| 1066 |
$task_actions = $task_definition.Actions |
| 1067 |
foreach ($action in $actions) { |
| 1068 |
$create_diff_string += "+action[0] = {`n +Type=$([TASK_ACTION_TYPE]::TASK_ACTION_EXEC),`n +Path=$($action.path)`n" |
| 1069 |
$task_action = $task_actions.Create([TASK_ACTION_TYPE]::TASK_ACTION_EXEC) |
| 1070 |
$task_action.Path = $action.path |
| 1071 |
if ($null -ne $action.arguments) { |
| 1072 |
$create_diff_string += " +Arguments=$($action.arguments)`n" |
| 1073 |
$task_action.Arguments = $action.arguments |
| 1074 |
} |
| 1075 |
if ($null -ne $action.working_directory) { |
| 1076 |
$create_diff_string += " +WorkingDirectory=$($action.working_directory)`n" |
| 1077 |
$task_action.WorkingDirectory = $action.working_directory |
| 1078 |
} |
| 1079 |
$create_diff_string += "+}`n" |
| 1080 |
} |
| 1081 |
|
| 1082 |
# Register the new task |
| 1083 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382577(v=vs.85).aspx |
| 1084 |
if ($check_mode) { |
| 1085 |
# Only validate the task in check mode |
| 1086 |
$task_creation_flags = [TASK_CREATION]::TASK_VALIDATE_ONLY |
| 1087 |
} |
| 1088 |
else { |
| 1089 |
# Create the task but do not fire it as we still need to configure it further below |
| 1090 |
$task_creation_flags = [TASK_CREATION]::TASK_CREATE -bor [TASK_CREATION]::TASK_IGNORE_REGISTRATION_TRIGGERS |
| 1091 |
} |
| 1092 |
|
| 1093 |
# folder doesn't exist, need to create |
| 1094 |
if ($null -eq $task_folder) { |
| 1095 |
$task_folder = $service.GetFolder("\") |
| 1096 |
try { |
| 1097 |
if (-not $check_mode) { |
| 1098 |
$task_folder = $task_folder.CreateFolder($path) |
| 1099 |
} |
| 1100 |
} |
| 1101 |
catch { |
| 1102 |
Fail-Json -obj $result -message "failed to create new folder at path '$path': $($_.Exception.Message)" |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
try { |
| 1107 |
$task = $task_folder.RegisterTaskDefinition($name, $task_definition, $task_creation_flags, $null, $null, $null) |
| 1108 |
} |
| 1109 |
catch { |
| 1110 |
Fail-Json -obj $result -message "failed to register new task definition: $($_.Exception.Message)" |
| 1111 |
} |
| 1112 |
if ($diff_mode) { |
| 1113 |
$result.diff.prepared = $create_diff_string |
| 1114 |
} |
| 1115 |
|
| 1116 |
$result.changed = $true |
| 1117 |
} |
| 1118 |
|
| 1119 |
# we cannot configure a task that was created above in check mode as it |
| 1120 |
# won't actually exist |
| 1121 |
if ($task) { |
| 1122 |
$task_definition = $task.Definition |
| 1123 |
$task_definition_xml = [xml]$task_definition.XmlText |
| 1124 |
|
| 1125 |
$action_changes = Compare-Action -task_definition $task_definition |
| 1126 |
$principal_changed = Compare-Principal -task_definition $task_definition -task_definition_xml $task_definition_xml |
| 1127 |
$reg_info_changed = Compare-RegistrationInfo -task_definition $task_definition |
| 1128 |
$settings_changed = Compare-Setting -task_definition $task_definition |
| 1129 |
$trigger_changes = Compare-Trigger -task_definition $task_definition |
| 1130 |
|
| 1131 |
# compile the diffs into one list with headers |
| 1132 |
$task_diff = [System.Collections.ArrayList]@() |
| 1133 |
if ($action_changes.Count -gt 0) { |
| 1134 |
[void]$task_diff.Add("[Actions]") |
| 1135 |
foreach ($action_change in $action_changes) { |
| 1136 |
[void]$task_diff.Add($action_change) |
| 1137 |
} |
| 1138 |
[void]$task_diff.Add("`n") |
| 1139 |
} |
| 1140 |
if ($principal_changed.Count -gt 0) { |
| 1141 |
[void]$task_diff.Add("[Principal]") |
| 1142 |
foreach ($principal_change in $principal_changed) { |
| 1143 |
[void]$task_diff.Add($principal_change) |
| 1144 |
} |
| 1145 |
[void]$task_diff.Add("`n") |
| 1146 |
} |
| 1147 |
if ($reg_info_changed.Count -gt 0) { |
| 1148 |
[void]$task_diff.Add("[Registration Info]") |
| 1149 |
foreach ($reg_info_change in $reg_info_changed) { |
| 1150 |
[void]$task_diff.Add($reg_info_change) |
| 1151 |
} |
| 1152 |
[void]$task_diff.Add("`n") |
| 1153 |
} |
| 1154 |
if ($settings_changed.Count -gt 0) { |
| 1155 |
[void]$task_diff.Add("[Settings]") |
| 1156 |
foreach ($settings_change in $settings_changed) { |
| 1157 |
[void]$task_diff.add($settings_change) |
| 1158 |
} |
| 1159 |
[void]$task_diff.Add("`n") |
| 1160 |
} |
| 1161 |
if ($trigger_changes.Count -gt 0) { |
| 1162 |
[void]$task_diff.Add("[Triggers]") |
| 1163 |
foreach ($trigger_change in $trigger_changes) { |
| 1164 |
[void]$task_diff.Add("$trigger_change") |
| 1165 |
} |
| 1166 |
[void]$task_diff.Add("`n") |
| 1167 |
} |
| 1168 |
|
| 1169 |
if ($null -ne $password -and (($update_password -eq $true) -or ($task_diff.Count -gt 0))) { |
| 1170 |
# because we can't compare the passwords we just need to reset it |
| 1171 |
$register_username = $username |
| 1172 |
$register_password = $password |
| 1173 |
$register_logon_type = $task_principal.LogonType |
| 1174 |
} |
| 1175 |
else { |
| 1176 |
# will inherit from the Principal property values |
| 1177 |
$register_username = $null |
| 1178 |
$register_password = $null |
| 1179 |
$register_logon_type = $null |
| 1180 |
} |
| 1181 |
|
| 1182 |
if ($task_diff.Count -gt 0 -or $null -ne $register_password) { |
| 1183 |
if ($check_mode) { |
| 1184 |
# Only validate the task in check mode |
| 1185 |
$task_creation_flags = [TASK_CREATION]::TASK_VALIDATE_ONLY |
| 1186 |
} |
| 1187 |
else { |
| 1188 |
# Create the task |
| 1189 |
$task_creation_flags = [TASK_CREATION]::TASK_CREATE_OR_UPDATE |
| 1190 |
} |
| 1191 |
try { |
| 1192 |
$task_folder.RegisterTaskDefinition( |
| 1193 |
$name, |
| 1194 |
$task_definition, |
| 1195 |
$task_creation_flags, |
| 1196 |
$register_username, |
| 1197 |
$register_password, |
| 1198 |
$register_logon_type |
| 1199 |
) | Out-Null |
| 1200 |
} |
| 1201 |
catch { |
| 1202 |
Fail-Json -obj $result -message "failed to modify scheduled task: $($_.Exception.Message)" |
| 1203 |
} |
| 1204 |
|
| 1205 |
$result.changed = $true |
| 1206 |
|
| 1207 |
if ($diff_mode) { |
| 1208 |
$changed_diff_text = $task_diff -join "`n" |
| 1209 |
if ($null -ne $result.diff.prepared) { |
| 1210 |
$diff_text = "$($result.diff.prepared)`n$changed_diff_text" |
| 1211 |
} |
| 1212 |
else { |
| 1213 |
$diff_text = $changed_diff_text |
| 1214 |
} |
| 1215 |
$result.diff.prepared = $diff_text.Trim() |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
Exit-Json -obj $result |