Skip to content

Commit

Permalink
Fix (definitions): Revise to use strings instead of here-strings for …
Browse files Browse the repository at this point in the history
…definition object action command arguments

This change will keep the definition object format in line with the design of task scheduler which only accepts a string for task action command arguments.

With the discovery made in 31679d2, it is now apparent that single-quotes cannot be used in task action command arguments. The reusability of command argument code is thus limited and renders little use for here-strings.

This change also partially reverts changes in 46e51c0.
  • Loading branch information
joeltimothyoh committed Sep 27, 2022
1 parent dc6413e commit f3879ac
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 337 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The properties of definition objects are based off the parameters of the followi
* [`New-ScheduledTaskSettingsSet`](https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtasksettingsset)
* [`New-ScheduledTaskPrincipal`](https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtaskprincipal)

Several parameters such as [`-At`](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtasktrigger#-at) of `New-ScheduledTaskTrigger` involve values whose types prevent them from being expressed purely in non-PowerShell code. The module accounts for such values by enabling them to be intuitively yet declaratively defined even in [`.json`](docs/samples/definitions/scheduledtasks/tasks.sample.json#L57-L70) format. Those values will be *converted* into their relevant PowerShell types as part of a [serialization process](src/ScheduledTaskManagement/Private/Serialize-DefinitionObject.ps1#L19-L27) right before task creation or application. Through so, scheduled tasks can be managed as code as part of the practice of [Infrastructure-as-Code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code).
Several parameters such as [`-At`](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtasktrigger#-at) of `New-ScheduledTaskTrigger` involve values whose types prevent them from being expressed purely in non-PowerShell code. The module accounts for such values by enabling them to be intuitively yet declaratively defined even in [`.json`](docs/samples/definitions/scheduledtasks/tasks.sample.json#L49-L61) format. Those values will be *converted* into their relevant PowerShell types as part of a [serialization process](src/ScheduledTaskManagement/Private/Serialize-DefinitionObject.ps1#L19-L27) right before task creation or application. Through so, scheduled tasks can be managed as code as part of the practice of [Infrastructure-as-Code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code).

Sample definition files can be found [here](docs/samples/definitions/scheduledtasks).

Expand Down
182 changes: 91 additions & 91 deletions docs/samples/definitions/scheduledtasks/tasks.sample.json
Original file line number Diff line number Diff line change
@@ -1,163 +1,163 @@
[
{
"TaskName": "MyTaskName1",
"Action": [
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-1)\\\"\"",
"Execute": "powershell"
},
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-2)\\\"\"",
"Execute": "powershell"
}
],
"TaskPath": "\\MyTaskFolder\\",
"Trigger": [
{
"AtStartup": true
},
{
"DaysInterval": 1,
"Daily": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 17,
"Minute": 30,
"Second": 0,
"Day": 30,
"Hour": 17,
"Month": 11
},
"Daily": true,
"DaysInterval": 1
}
}
],
"Action": [
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-1)\\\"\""
},
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-2)\\\"\""
}
],
"Settings": {
"DontStopIfGoingOnBatteries": true,
"Disable": false,
"AllowStartIfOnBatteries": true
},
"Principal": {
"UserId": "myusername",
"LogonType": "S4U",
"UserId": "myusername",
"RunLevel": "Highest"
},
"TaskPath": "\\MyTaskFolder\\"
"Settings": {
"Disable": false,
"DontStopIfGoingOnBatteries": true,
"AllowStartIfOnBatteries": true
}
},
{
"TaskName": "MyTaskName2",
"Action": [
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-1)\\\"\"",
"Execute": "powershell"
},
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-2)\\\"\"",
"Execute": "powershell"
}
],
"TaskPath": "\\MyTaskFolder\\",
"Trigger": [
{
"Once": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 14,
"Minute": 0,
"Second": 0,
"Day": 30,
"Hour": 14,
"Month": 11
},
"Once": true,
"RepetitionInterval": {
"Minute": 0,
"Second": 0,
"Hour": 12,
"Second": 0
"Minute": 0
}
}
],
"Settings": {
"DontStopIfGoingOnBatteries": true,
"Disable": false,
"AllowStartIfOnBatteries": true
},
"Action": [
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-1)\\\"\""
},
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-2)\\\"\""
}
],
"Principal": {
"UserId": "NT AUTHORITY\\SYSTEM",
"LogonType": "S4U",
"UserId": "NT AUTHORITY\\SYSTEM",
"RunLevel": "Limited"
},
"TaskPath": "\\MyTaskFolder\\"
"Settings": {
"Disable": false,
"DontStopIfGoingOnBatteries": true,
"AllowStartIfOnBatteries": true
}
},
{
"TaskName": "MyTaskName3",
"Action": [
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-1)\\\"\"",
"Execute": "powershell"
},
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-2)\\\"\"",
"Execute": "powershell"
},
{
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-3)\\\"\"",
"Execute": "powershell"
}
],
"TaskPath": "\\MyTaskFolder\\",
"Trigger": [
{
"DaysInterval": 1,
"Daily": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 10,
"Minute": 0,
"Second": 0,
"Day": 30,
"Hour": 10,
"Month": 11
},
"Daily": true,
"DaysInterval": 1
}
},
{
"DaysInterval": 1,
"Daily": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 14,
"Minute": 0,
"Second": 0,
"Day": 30,
"Hour": 14,
"Month": 11
},
"Daily": true,
"DaysInterval": 1
}
},
{
"DaysInterval": 1,
"Daily": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 18,
"Minute": 0,
"Second": 0,
"Day": 30,
"Hour": 18,
"Month": 11
},
"Daily": true,
"DaysInterval": 1
}
},
{
"DaysInterval": 1,
"Daily": true,
"At": {
"Day": 30,
"Second": 0,
"Year": 1999,
"Hour": 22,
"Minute": 0,
"Second": 0,
"Day": 30,
"Hour": 22,
"Month": 11
},
"Daily": true,
"DaysInterval": 1
}
}
],
"Action": [
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-1)\\\"\""
},
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-2)\\\"\""
},
{
"Execute": "powershell",
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-3)\\\"\""
}
],
"Settings": {
"DontStopIfGoingOnBatteries": true,
"Disable": false,
"AllowStartIfOnBatteries": true
},
"Principal": {
"UserId": "myusername",
"LogonType": "S4U",
"UserId": "myusername",
"RunLevel": "Highest"
},
"TaskPath": "\\MyTaskFolder\\"
"Settings": {
"Disable": false,
"DontStopIfGoingOnBatteries": true,
"AllowStartIfOnBatteries": true
}
}
]
28 changes: 7 additions & 21 deletions docs/samples/definitions/scheduledtasks/tasks.sample.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,11 @@
Action = @(
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-1)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-1)\""'
}
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-2)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-2)\""'
}
)
Settings = @{
Expand Down Expand Up @@ -103,15 +99,11 @@
Action = @(
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-1)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-1)\""'
}
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-2)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-2)\""'
}
)
Settings = @{
Expand Down Expand Up @@ -221,21 +213,15 @@
Action = @(
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-1)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-1)\""'
}
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-2)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-2)\""'
}
@{
Execute = 'powershell'
Argument = @'
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-3)\""
'@
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-3)\""'
}
)
Settings = @{
Expand Down
Loading

0 comments on commit f3879ac

Please sign in to comment.