Skip to content

Commit f3879ac

Browse files
committed
Fix (definitions): Revise to use strings instead of here-strings for 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.
1 parent dc6413e commit f3879ac

7 files changed

Lines changed: 295 additions & 337 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The properties of definition objects are based off the parameters of the followi
5151
* [`New-ScheduledTaskSettingsSet`](https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtasksettingsset)
5252
* [`New-ScheduledTaskPrincipal`](https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtaskprincipal)
5353

54-
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).
54+
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).
5555

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

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,163 @@
11
[
22
{
33
"TaskName": "MyTaskName1",
4-
"Action": [
5-
{
6-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-1)\\\"\"",
7-
"Execute": "powershell"
8-
},
9-
{
10-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-2)\\\"\"",
11-
"Execute": "powershell"
12-
}
13-
],
4+
"TaskPath": "\\MyTaskFolder\\",
145
"Trigger": [
156
{
167
"AtStartup": true
178
},
189
{
10+
"DaysInterval": 1,
11+
"Daily": true,
1912
"At": {
13+
"Day": 30,
14+
"Second": 0,
2015
"Year": 1999,
21-
"Hour": 17,
2216
"Minute": 30,
23-
"Second": 0,
24-
"Day": 30,
17+
"Hour": 17,
2518
"Month": 11
26-
},
27-
"Daily": true,
28-
"DaysInterval": 1
19+
}
20+
}
21+
],
22+
"Action": [
23+
{
24+
"Execute": "powershell",
25+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-1)\\\"\""
26+
},
27+
{
28+
"Execute": "powershell",
29+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-1-2)\\\"\""
2930
}
3031
],
31-
"Settings": {
32-
"DontStopIfGoingOnBatteries": true,
33-
"Disable": false,
34-
"AllowStartIfOnBatteries": true
35-
},
3632
"Principal": {
37-
"UserId": "myusername",
3833
"LogonType": "S4U",
34+
"UserId": "myusername",
3935
"RunLevel": "Highest"
4036
},
41-
"TaskPath": "\\MyTaskFolder\\"
37+
"Settings": {
38+
"Disable": false,
39+
"DontStopIfGoingOnBatteries": true,
40+
"AllowStartIfOnBatteries": true
41+
}
4242
},
4343
{
4444
"TaskName": "MyTaskName2",
45-
"Action": [
46-
{
47-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-1)\\\"\"",
48-
"Execute": "powershell"
49-
},
50-
{
51-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-2)\\\"\"",
52-
"Execute": "powershell"
53-
}
54-
],
45+
"TaskPath": "\\MyTaskFolder\\",
5546
"Trigger": [
5647
{
48+
"Once": true,
5749
"At": {
50+
"Day": 30,
51+
"Second": 0,
5852
"Year": 1999,
59-
"Hour": 14,
6053
"Minute": 0,
61-
"Second": 0,
62-
"Day": 30,
54+
"Hour": 14,
6355
"Month": 11
6456
},
65-
"Once": true,
6657
"RepetitionInterval": {
67-
"Minute": 0,
58+
"Second": 0,
6859
"Hour": 12,
69-
"Second": 0
60+
"Minute": 0
7061
}
7162
}
7263
],
73-
"Settings": {
74-
"DontStopIfGoingOnBatteries": true,
75-
"Disable": false,
76-
"AllowStartIfOnBatteries": true
77-
},
64+
"Action": [
65+
{
66+
"Execute": "powershell",
67+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-1)\\\"\""
68+
},
69+
{
70+
"Execute": "powershell",
71+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-2-2)\\\"\""
72+
}
73+
],
7874
"Principal": {
79-
"UserId": "NT AUTHORITY\\SYSTEM",
8075
"LogonType": "S4U",
76+
"UserId": "NT AUTHORITY\\SYSTEM",
8177
"RunLevel": "Limited"
8278
},
83-
"TaskPath": "\\MyTaskFolder\\"
79+
"Settings": {
80+
"Disable": false,
81+
"DontStopIfGoingOnBatteries": true,
82+
"AllowStartIfOnBatteries": true
83+
}
8484
},
8585
{
8686
"TaskName": "MyTaskName3",
87-
"Action": [
88-
{
89-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-1)\\\"\"",
90-
"Execute": "powershell"
91-
},
92-
{
93-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-2)\\\"\"",
94-
"Execute": "powershell"
95-
},
96-
{
97-
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-3)\\\"\"",
98-
"Execute": "powershell"
99-
}
100-
],
87+
"TaskPath": "\\MyTaskFolder\\",
10188
"Trigger": [
10289
{
90+
"DaysInterval": 1,
91+
"Daily": true,
10392
"At": {
93+
"Day": 30,
94+
"Second": 0,
10495
"Year": 1999,
105-
"Hour": 10,
10696
"Minute": 0,
107-
"Second": 0,
108-
"Day": 30,
97+
"Hour": 10,
10998
"Month": 11
110-
},
111-
"Daily": true,
112-
"DaysInterval": 1
99+
}
113100
},
114101
{
102+
"DaysInterval": 1,
103+
"Daily": true,
115104
"At": {
105+
"Day": 30,
106+
"Second": 0,
116107
"Year": 1999,
117-
"Hour": 14,
118108
"Minute": 0,
119-
"Second": 0,
120-
"Day": 30,
109+
"Hour": 14,
121110
"Month": 11
122-
},
123-
"Daily": true,
124-
"DaysInterval": 1
111+
}
125112
},
126113
{
114+
"DaysInterval": 1,
115+
"Daily": true,
127116
"At": {
117+
"Day": 30,
118+
"Second": 0,
128119
"Year": 1999,
129-
"Hour": 18,
130120
"Minute": 0,
131-
"Second": 0,
132-
"Day": 30,
121+
"Hour": 18,
133122
"Month": 11
134-
},
135-
"Daily": true,
136-
"DaysInterval": 1
123+
}
137124
},
138125
{
126+
"DaysInterval": 1,
127+
"Daily": true,
139128
"At": {
129+
"Day": 30,
130+
"Second": 0,
140131
"Year": 1999,
141-
"Hour": 22,
142132
"Minute": 0,
143-
"Second": 0,
144-
"Day": 30,
133+
"Hour": 22,
145134
"Month": 11
146-
},
147-
"Daily": true,
148-
"DaysInterval": 1
135+
}
136+
}
137+
],
138+
"Action": [
139+
{
140+
"Execute": "powershell",
141+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-1)\\\"\""
142+
},
143+
{
144+
"Execute": "powershell",
145+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-2)\\\"\""
146+
},
147+
{
148+
"Execute": "powershell",
149+
"Argument": "-NonInteractive -NoProfile -NoLogo -Command \"New-Item \\\"$env:TEMP\\$(Get-Date -UFormat .%s-3-3)\\\"\""
149150
}
150151
],
151-
"Settings": {
152-
"DontStopIfGoingOnBatteries": true,
153-
"Disable": false,
154-
"AllowStartIfOnBatteries": true
155-
},
156152
"Principal": {
157-
"UserId": "myusername",
158153
"LogonType": "S4U",
154+
"UserId": "myusername",
159155
"RunLevel": "Highest"
160156
},
161-
"TaskPath": "\\MyTaskFolder\\"
157+
"Settings": {
158+
"Disable": false,
159+
"DontStopIfGoingOnBatteries": true,
160+
"AllowStartIfOnBatteries": true
161+
}
162162
}
163163
]

docs/samples/definitions/scheduledtasks/tasks.sample.ps1

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@
5151
Action = @(
5252
@{
5353
Execute = 'powershell'
54-
Argument = @'
55-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-1)\""
56-
'@
54+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-1)\""'
5755
}
5856
@{
5957
Execute = 'powershell'
60-
Argument = @'
61-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-2)\""
62-
'@
58+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-1-2)\""'
6359
}
6460
)
6561
Settings = @{
@@ -103,15 +99,11 @@
10399
Action = @(
104100
@{
105101
Execute = 'powershell'
106-
Argument = @'
107-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-1)\""
108-
'@
102+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-1)\""'
109103
}
110104
@{
111105
Execute = 'powershell'
112-
Argument = @'
113-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-2)\""
114-
'@
106+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-2-2)\""'
115107
}
116108
)
117109
Settings = @{
@@ -221,21 +213,15 @@
221213
Action = @(
222214
@{
223215
Execute = 'powershell'
224-
Argument = @'
225-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-1)\""
226-
'@
216+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-1)\""'
227217
}
228218
@{
229219
Execute = 'powershell'
230-
Argument = @'
231-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-2)\""
232-
'@
220+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-2)\""'
233221
}
234222
@{
235223
Execute = 'powershell'
236-
Argument = @'
237-
-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-3)\""
238-
'@
224+
Argument = '-NonInteractive -NoProfile -NoLogo -Command "New-Item \"$env:TEMP\$(Get-Date -UFormat .%s-3-3)\""'
239225
}
240226
)
241227
Settings = @{

0 commit comments

Comments
 (0)