1
1
$Script :IsPowerShellCore = $PSVersionTable.PSEdition -eq ' Core'
2
2
3
- if ($Script :IsPowerShellCore )
4
- {
5
- if ($IsWindows )
6
- {
3
+ if ($Script :IsPowerShellCore ) {
4
+ if ($IsWindows ) {
7
5
Import-Module - Name ' PSDesiredStateConfiguration' - RequiredVersion 1.1 - UseWindowsPowerShell - WarningAction SilentlyContinue
8
6
}
9
7
Import-Module - Name ' PSDesiredStateConfiguration' - MinimumVersion 2.0 .7 - Prefix ' Pwsh' - WarningAction SilentlyContinue
8
+ } else {
9
+ Import-Module - Name ' PSDesiredStateConfiguration' - RequiredVersion 1.1 - WarningAction SilentlyContinue
10
10
}
11
11
12
12
function Write-DscTrace {
@@ -22,8 +22,7 @@ function Write-DscTrace {
22
22
$host.ui.WriteErrorLine ($trace )
23
23
}
24
24
25
- function Build-DscConfigDocument
26
- {
25
+ function Build-DscConfigDocument {
27
26
[CmdletBinding ()]
28
27
[OutputType ([System.Collections.Specialized.OrderedDictionary ])]
29
28
param
@@ -42,17 +41,15 @@ function Build-DscConfigDocument
42
41
# convert object to hashtable(s)
43
42
$dscObjects = ConvertTo-DscObject @PSBoundParameters - ErrorAction SilentlyContinue
44
43
45
- if (-not $dscObjects -or $dscObjects.Count -eq 0 )
46
- {
44
+ if (-not $dscObjects -or $dscObjects.Count -eq 0 ) {
47
45
" No DSC objects found in the provided content." | Write-DscTrace - Operation Error
48
46
exit 1
49
47
}
50
48
51
49
# store all resources in variables
52
50
$resources = [System.Collections.Generic.List [object ]]::new()
53
51
54
- foreach ($dscObject in $dscObjects )
55
- {
52
+ foreach ($dscObject in $dscObjects ) {
56
53
$resource = [PSCustomObject ]@ {
57
54
name = $dscObject.ResourceInstanceName
58
55
type = (" {0}/{1}" -f $dscObject.ModuleName , $dscObject.ResourceName )
@@ -61,24 +58,20 @@ function Build-DscConfigDocument
61
58
62
59
$properties = [ordered ]@ {}
63
60
64
- foreach ($dscObjectProperty in $dscObject.GetEnumerator ())
65
- {
66
- if ($dscObjectProperty.Key -notin @ (' ResourceInstanceName' , ' ResourceName' , ' ModuleName' , ' DependsOn' , ' ConfigurationName' , ' Type' ))
67
- {
61
+ foreach ($dscObjectProperty in $dscObject.GetEnumerator ()) {
62
+ if ($dscObjectProperty.Key -notin @ (' ResourceInstanceName' , ' ResourceName' , ' ModuleName' , ' DependsOn' , ' ConfigurationName' , ' Type' )) {
68
63
$properties.Add ($dscObjectProperty.Key , $dscObjectProperty.Value )
69
64
}
70
65
}
71
66
72
67
# add properties
73
68
$resource.properties = $properties
74
69
75
- if ($dscObject.ContainsKey (' DependsOn' ) -and $dscObject.DependsOn )
76
- {
70
+ if ($dscObject.ContainsKey (' DependsOn' ) -and $dscObject.DependsOn ) {
77
71
$dependsOnKeys = $dscObject.DependsOn.Split (" ]" ).Replace(" [" , " " )
78
72
79
73
$previousGroupHash = $dscObjects | Where-Object { $_.ResourceName -eq $dependsOnKeys [0 ] -and $_.ResourceInstanceName -eq $dependsOnKeys [1 ] }
80
- if ($previousGroupHash )
81
- {
74
+ if ($previousGroupHash ) {
82
75
$dependsOnString = " [resourceId('$ ( " {0}/{1}" -f $previousGroupHash.ModuleName , $previousGroupHash.ResourceName ) ','$ ( $previousGroupHash.ResourceInstanceName ) ')]"
83
76
84
77
# add it to the object
@@ -94,8 +87,7 @@ function Build-DscConfigDocument
94
87
return $configurationDocument
95
88
}
96
89
97
- function ConvertTo-DscObject
98
- {
90
+ function ConvertTo-DscObject {
99
91
[CmdletBinding ()]
100
92
param
101
93
(
@@ -110,33 +102,26 @@ function ConvertTo-DscObject
110
102
111
103
# Remove the module version information.
112
104
$start = $Content.ToLower ().IndexOf(' import-dscresource' )
113
- if ($start -ge 0 )
114
- {
105
+ if ($start -ge 0 ) {
115
106
$end = $Content.IndexOf (" `n " , $start )
116
- if ($end -gt $start )
117
- {
107
+ if ($end -gt $start ) {
118
108
$start = $Content.ToLower ().IndexOf(" -moduleversion" , $start )
119
- if ($start -ge 0 -and $start -lt $end )
120
- {
109
+ if ($start -ge 0 -and $start -lt $end ) {
121
110
$Content = $Content.Remove ($start , $end - $start )
122
111
}
123
112
}
124
113
}
125
114
126
115
# Rename the configuration node to ensure a valid name is used.
127
116
$start = $Content.ToLower ().IndexOf(" `n configuration" )
128
- if ($start -lt 0 )
129
- {
117
+ if ($start -lt 0 ) {
130
118
$start = $Content.ToLower ().IndexOf(' configuration ' )
131
119
}
132
- if ($start -ge 0 )
133
- {
120
+ if ($start -ge 0 ) {
134
121
$end = $Content.IndexOf (" `n " , $start )
135
- if ($end -gt $start )
136
- {
122
+ if ($end -gt $start ) {
137
123
$start = $Content.ToLower ().IndexOf(' ' , $start + 1 )
138
- if ($start -ge 0 -and $start -lt $end )
139
- {
124
+ if ($start -ge 0 -and $start -lt $end ) {
140
125
$Content = $Content.Remove ($start , $end - $start )
141
126
$Content = $Content.Insert ($start , " TempDSCParserConfiguration" )
142
127
}
@@ -151,29 +136,21 @@ function ConvertTo-DscObject
151
136
# Retrieve information about the DSC Modules imported in the config
152
137
# and get the list of their associated resources.
153
138
$ModulesToLoad = @ ()
154
- foreach ($statement in $config.body.ScriptBlock.EndBlock.Statements )
155
- {
139
+ foreach ($statement in $config.body.ScriptBlock.EndBlock.Statements ) {
156
140
if ($null -ne $statement.CommandElements -and $null -ne $statement.CommandElements [0 ].Value -and `
157
- $statement.CommandElements [0 ].Value -eq ' Import-DSCResource' )
158
- {
141
+ $statement.CommandElements [0 ].Value -eq ' Import-DSCResource' ) {
159
142
$currentModule = @ {}
160
- for ($i = 0 ; $i -le $statement.CommandElements.Count ; $i ++ )
161
- {
143
+ for ($i = 0 ; $i -le $statement.CommandElements.Count ; $i ++ ) {
162
144
if ($statement.CommandElements [$i ].ParameterName -eq ' ModuleName' -and `
163
- ($i + 1 ) -lt $statement.CommandElements.Count )
164
- {
145
+ ($i + 1 ) -lt $statement.CommandElements.Count ) {
165
146
$moduleName = $statement.CommandElements [$i + 1 ].Value
166
147
$currentModule.Add (' ModuleName' , $moduleName )
167
- }
168
- elseif ($statement.CommandElements [$i ].ParameterName -eq ' Module' -and `
169
- ($i + 1 ) -lt $statement.CommandElements.Count )
170
- {
148
+ } elseif ($statement.CommandElements [$i ].ParameterName -eq ' Module' -and `
149
+ ($i + 1 ) -lt $statement.CommandElements.Count ) {
171
150
$moduleName = $statement.CommandElements [$i + 1 ].Value
172
151
$currentModule.Add (' ModuleName' , $moduleName )
173
- }
174
- elseif ($statement.CommandElements [$i ].ParameterName -eq ' ModuleVersion' -and `
175
- ($i + 1 ) -lt $statement.CommandElements.Count )
176
- {
152
+ } elseif ($statement.CommandElements [$i ].ParameterName -eq ' ModuleVersion' -and `
153
+ ($i + 1 ) -lt $statement.CommandElements.Count ) {
177
154
$moduleVersion = $statement.CommandElements [$i + 1 ].Value
178
155
$currentModule.Add (' ModuleVersion' , $moduleVersion )
179
156
}
@@ -182,36 +159,27 @@ function ConvertTo-DscObject
182
159
}
183
160
}
184
161
$DSCResources = @ ()
185
- foreach ($moduleToLoad in $ModulesToLoad )
186
- {
162
+ foreach ($moduleToLoad in $ModulesToLoad ) {
187
163
$loadedModuleTest = Get-Module - Name $moduleToLoad.ModuleName - ListAvailable | Where-Object - FilterScript { $_.Version -eq $moduleToLoad.ModuleVersion }
188
164
189
- if ($null -eq $loadedModuleTest -and -not [System.String ]::IsNullOrEmpty($moduleToLoad.ModuleVersion ))
190
- {
165
+ if ($null -eq $loadedModuleTest -and -not [System.String ]::IsNullOrEmpty($moduleToLoad.ModuleVersion )) {
191
166
" Module {$ ( $moduleToLoad.ModuleName ) } version {$ ( $moduleToLoad.ModuleVersion ) } specified in the configuration isn't installed on the machine/agent. Install it by running: Install-Module -Name '$ ( $moduleToLoad.ModuleName ) ' -RequiredVersion '$ ( $moduleToLoad.ModuleVersion ) '" | Write-DscTrace - Operation Error
192
167
exit 1
193
- }
194
- else
195
- {
196
- if ($Script :IsPowerShellCore )
197
- {
168
+ } else {
169
+ if ($Script :IsPowerShellCore ) {
198
170
$currentResources = Get-PwshDscResource - Module $moduleToLoad.ModuleName
199
- }
200
- else
201
- {
171
+ } else {
202
172
$currentResources = Get-DSCResource - Module $moduleToLoad.ModuleName
203
173
}
204
174
205
- if (-not [System.String ]::IsNullOrEmpty($moduleToLoad.ModuleVersion ))
206
- {
175
+ if (-not [System.String ]::IsNullOrEmpty($moduleToLoad.ModuleVersion )) {
207
176
$currentResources = $currentResources | Where-Object - FilterScript { $_.Version -eq $moduleToLoad.ModuleVersion }
208
177
}
209
178
$DSCResources += $currentResources
210
179
}
211
180
}
212
181
213
- if ($DSCResources.Count -eq 0 )
214
- {
182
+ if ($DSCResources.Count -eq 0 ) {
215
183
" No DSC resources found in the imported modules." | Write-DscTrace - Operation Error
216
184
exit 1
217
185
}
@@ -220,21 +188,17 @@ function ConvertTo-DscObject
220
188
# Body.ScriptBlock is the part after "Configuration <InstanceName> {"
221
189
# EndBlock is the actual code within that Configuration block
222
190
# Find the first DynamicKeywordStatement that has a word "Node" in it, find all "NamedBlockAst" elements, these are the DSC resource definitions
223
- try
224
- {
191
+ try {
225
192
$resourceInstances = $Config.Body.ScriptBlock.EndBlock.Statements.Find ({ $Args [0 ].GetType().Name -eq ' DynamicKeywordStatementAst' -and $Args [0 ].CommandElements[0 ].StringConstantType -eq ' BareWord' -and $Args [0 ].CommandElements[0 ].Value -eq ' Node' }, $False ).commandElements[2 ].ScriptBlock.Find({ $Args [0 ].GetType().Name -eq ' NamedBlockAst' }, $False ).Statements
226
- }
227
- catch
228
- {
193
+ } catch {
229
194
$resourceInstances = $Config.Body.ScriptBlock.EndBlock.Statements | Where-Object - FilterScript { $null -ne $_.CommandElements -and $_.CommandElements [0 ].Value -ne ' Import-DscResource' }
230
195
}
231
196
232
197
# Get the name of the configuration.
233
198
$configurationName = $Config.InstanceName.Value
234
199
235
200
$totalCount = 1
236
- foreach ($resource in $resourceInstances )
237
- {
201
+ foreach ($resource in $resourceInstances ) {
238
202
$currentResourceInfo = @ {}
239
203
240
204
# CommandElements
@@ -257,61 +221,41 @@ function ConvertTo-DscObject
257
221
$currentResource = $DSCResources | Where-Object - FilterScript { $_.Name -eq $resourceType }
258
222
259
223
# Loop through all the key/pair value
260
- foreach ($keyValuePair in $resource.CommandElements [2 ].KeyValuePairs)
261
- {
224
+ foreach ($keyValuePair in $resource.CommandElements [2 ].KeyValuePairs) {
262
225
$isVariable = $false
263
226
$key = $keyValuePair.Item1.Value
264
227
265
- if ($null -ne $keyValuePair.Item2.PipelineElements )
266
- {
267
- if ($null -eq $keyValuePair.Item2.PipelineElements.Expression.Value )
268
- {
269
- if ($null -ne $keyValuePair.Item2.PipelineElements.Expression )
270
- {
271
- if ($keyValuePair.Item2.PipelineElements.Expression.StaticType.Name -eq ' Object[]' )
272
- {
228
+ if ($null -ne $keyValuePair.Item2.PipelineElements ) {
229
+ if ($null -eq $keyValuePair.Item2.PipelineElements.Expression.Value ) {
230
+ if ($null -ne $keyValuePair.Item2.PipelineElements.Expression ) {
231
+ if ($keyValuePair.Item2.PipelineElements.Expression.StaticType.Name -eq ' Object[]' ) {
273
232
$value = $keyValuePair.Item2.PipelineElements.Expression.SubExpression
274
233
$newValue = @ ()
275
- foreach ($expression in $value.Statements.PipelineElements.Expression )
276
- {
277
- if ($null -ne $expression.Elements )
278
- {
279
- foreach ($element in $expression.Elements )
280
- {
281
- if ($null -ne $element.VariablePath )
282
- {
234
+ foreach ($expression in $value.Statements.PipelineElements.Expression ) {
235
+ if ($null -ne $expression.Elements ) {
236
+ foreach ($element in $expression.Elements ) {
237
+ if ($null -ne $element.VariablePath ) {
283
238
$newValue += " `$ " + $element.VariablePath.ToString ()
284
- }
285
- elseif ($null -ne $element.Value )
286
- {
239
+ } elseif ($null -ne $element.Value ) {
287
240
$newValue += $element.Value
288
241
}
289
242
}
290
- }
291
- else
292
- {
243
+ } else {
293
244
$newValue += $expression.Value
294
245
}
295
246
}
296
247
$value = $newValue
297
- }
298
- else
299
- {
248
+ } else {
300
249
$value = $keyValuePair.Item2.PipelineElements.Expression.ToString ()
301
250
}
302
- }
303
- else
304
- {
251
+ } else {
305
252
$value = $keyValuePair.Item2.PipelineElements.Parent.ToString ()
306
253
}
307
254
308
- if ($value.GetType ().Name -eq ' String' -and $value.StartsWith (' $' ))
309
- {
255
+ if ($value.GetType ().Name -eq ' String' -and $value.StartsWith (' $' )) {
310
256
$isVariable = $true
311
257
}
312
- }
313
- else
314
- {
258
+ } else {
315
259
$value = $keyValuePair.Item2.PipelineElements.Expression.Value
316
260
}
317
261
}
@@ -323,19 +267,16 @@ function ConvertTo-DscObject
323
267
# If the value type is null, then the parameter doesn't exist
324
268
# in the resource's schema and we throw a warning
325
269
$propertyFound = $true
326
- if ($null -eq $valueType )
327
- {
270
+ if ($null -eq $valueType ) {
328
271
$propertyFound = $false
329
272
}
330
273
331
- if ($propertyFound )
332
- {
274
+ if ($propertyFound ) {
333
275
# If the current property is not a CIMInstance
334
276
if (-not $valueType.StartsWith (' [MSFT_' ) -and `
335
277
$valueType -ne ' [string]' -and `
336
278
$valueType -ne ' [string[]]' -and `
337
- -not $isVariable )
338
- {
279
+ -not $isVariable ) {
339
280
# Try to parse the value based on the retrieved type.
340
281
$scriptBlock = @"
341
282
`$ typeStaticMethods = $valueType | gm -static
@@ -345,44 +286,29 @@ function ConvertTo-DscObject
345
286
}
346
287
"@
347
288
Invoke-Expression - Command $scriptBlock | Out-Null
348
- }
349
- elseif ($valueType -eq ' [String]' -or $isVariable )
350
- {
351
- if ($isVariable -and [Boolean ]::TryParse($value.TrimStart (' $' ), [ref ][Boolean ]))
352
- {
353
- if ($value -eq " `$ true" )
354
- {
289
+ } elseif ($valueType -eq ' [String]' -or $isVariable ) {
290
+ if ($isVariable -and [Boolean ]::TryParse($value.TrimStart (' $' ), [ref ][Boolean ])) {
291
+ if ($value -eq " `$ true" ) {
355
292
$value = $true
356
- }
357
- else
358
- {
293
+ } else {
359
294
$value = $false
360
295
}
361
- }
362
- else
363
- {
296
+ } else {
364
297
$value = $value
365
298
}
366
- }
367
- elseif ($valueType -eq ' [string[]]' )
368
- {
299
+ } elseif ($valueType -eq ' [string[]]' ) {
369
300
# If the property is an array but there's only one value
370
301
# specified as a string (not specifying the @()) then
371
302
# we need to create the array.
372
- if ($value.GetType ().Name -eq ' String' -and -not $value.StartsWith (' @(' ))
373
- {
303
+ if ($value.GetType ().Name -eq ' String' -and -not $value.StartsWith (' @(' )) {
374
304
$value = @ ($value )
375
305
}
376
- }
377
- else
378
- {
306
+ } else {
379
307
$isArray = $false
380
- if ($keyValuePair.Item2.ToString ().StartsWith(' @(' ))
381
- {
308
+ if ($keyValuePair.Item2.ToString ().StartsWith(' @(' )) {
382
309
$isArray = $true
383
310
}
384
- if ($isArray )
385
- {
311
+ if ($isArray ) {
386
312
$value = @ ($value )
387
313
}
388
314
}
0 commit comments