You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DSCResources/CommonResourceHelper.psm1
+80-40Lines changed: 80 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -38,13 +38,21 @@ function Get-LocalizedData
38
38
return$localizedData
39
39
}
40
40
41
+
<#
42
+
.SYNOPSIS
43
+
Removes common parameters from a hashtable
44
+
.DESCRIPTION
45
+
This function serves the purpose of removing common parameters and option common parameters from a parameter hashtable
46
+
.PARAMETERHashtable
47
+
The parameter hashtable that should be pruned
48
+
#>
41
49
functionRemove-CommonParameter
42
50
{
43
51
[OutputType([hashtable])]
44
52
[cmdletbinding()]
45
53
param
46
54
(
47
-
[Parameter(Mandatory)]
55
+
[Parameter(Mandatory=$true)]
48
56
[hashtable]
49
57
$Hashtable
50
58
)
@@ -57,26 +65,41 @@ function Remove-CommonParameter
57
65
$inputClone.Remove($_)
58
66
}
59
67
60
-
$inputClone
68
+
return$inputClone
61
69
}
62
70
71
+
<#
72
+
.SYNOPSIS
73
+
Tests the status of DSC resource parameters
74
+
.DESCRIPTION
75
+
This function tests the parameter status of DSC resource parameters against the current values present on the system
76
+
.PARAMETERCurrentValues
77
+
A hashtable with the current values on the system, obtained by e.g. Get-TargetResource
78
+
.PARAMETERDesiredValues
79
+
The hashtable of desired values
80
+
.PARAMETERValuesToCheck
81
+
The values to check if not all values should be checked
82
+
.PARAMETERTurnOffTypeChecking
83
+
Indicates that the type of the parameter should not be checked
84
+
#>
63
85
functionTest-DscParameterState
64
86
{
65
87
[CmdletBinding()]
66
88
param
67
89
(
68
-
[Parameter(Mandatory)]
90
+
[Parameter(Mandatory=$true)]
69
91
[hashtable]
70
92
$CurrentValues,
71
93
72
-
[Parameter(Mandatory)]
94
+
[Parameter(Mandatory=$true)]
73
95
[object]
74
96
$DesiredValues,
75
97
76
98
[string[]]
77
99
$ValuesToCheck,
78
100
79
-
[switch]$TurnOffTypeChecking
101
+
[switch]
102
+
$TurnOffTypeChecking
80
103
)
81
104
82
105
$returnValue=$true
@@ -85,19 +108,19 @@ function Test-DscParameterState
85
108
86
109
if ($DesiredValues.GetType().FullName -notin$types)
87
110
{
88
-
throw ("Property 'DesiredValues' in Test-DscParameterState must be either a Hashtable or CimInstance. Type detected was $($DesiredValues.GetType().Name)")
111
+
throw ("Property 'DesiredValues' in Test-DscParameterState must be either a Hashtable or CimInstance. Type detected was $($DesiredValues.GetType().FullName)")
89
112
}
90
113
91
-
if ($DesiredValues.GetType().FullName -eq'Microsoft.Management.Infrastructure.CimInstance'-and-not$ValuesToCheck)
114
+
if ($DesiredValues-is [Microsoft.Management.Infrastructure.CimInstance]-and-not$ValuesToCheck)
92
115
{
93
116
throw ("If 'DesiredValues' is a CimInstance then property 'ValuesToCheck' must contain a value")
@@ -106,13 +129,15 @@ function Test-DscParameterState
106
129
107
130
foreach ($keyin$keyList)
108
131
{
109
-
if ($null-ne$DesiredValuesClean.$key)
132
+
if ($null-ne$desiredValuesClean.$key)
110
133
{
111
-
$desiredType=$DesiredValuesClean.$key.GetType()
134
+
$desiredType=$desiredValuesClean.$key.GetType()
112
135
}
113
136
else
114
137
{
115
-
$desiredType= [psobject]@{ Name='Unknown' }
138
+
$desiredType= [psobject]@{
139
+
Name='Unknown'
140
+
}
116
141
}
117
142
118
143
if ($null-ne$CurrentValues.$key)
@@ -121,64 +146,66 @@ function Test-DscParameterState
121
146
}
122
147
else
123
148
{
124
-
$currentType= [psobject]@{ Name='Unknown' }
149
+
$currentType= [psobject]@{
150
+
Name='Unknown'
151
+
}
125
152
}
126
153
127
154
if ($currentType.Name-ne'Unknown'-and$desiredType.Name-eq'PSCredential')
128
155
{
129
156
# This is a credential object. Compare only the user name
130
-
if ($currentType.Name-eq'PSCredential'-and$CurrentValues.$key.UserName-eq$DesiredValuesClean.$key.UserName)
157
+
if ($currentType.Name-eq'PSCredential'-and$CurrentValues.$key.UserName-eq$desiredValuesClean.$key.UserName)
131
158
{
132
-
Write-Verbose-Message ('MATCH: PSCredential username match. Current state is {0} and desired state is {1}'-f$CurrentValues.$key.UserName,$DesiredValuesClean.$key.UserName)
159
+
Write-Verbose-Message ('MATCH: PSCredential username match. Current state is {0} and desired state is {1}'-f$CurrentValues.$key.UserName,$desiredValuesClean.$key.UserName)
133
160
continue
134
161
}
135
162
else
136
163
{
137
-
Write-Verbose-Message ('NOTMATCH: PSCredential username mismatch. Current state is {0} and desired state is {1}'-f$CurrentValues.$key.UserName,$DesiredValuesClean.$key.UserName)
164
+
Write-Verbose-Message ('NOTMATCH: PSCredential username mismatch. Current state is {0} and desired state is {1}'-f$CurrentValues.$key.UserName,$desiredValuesClean.$key.UserName)
138
165
$returnValue=$false
139
166
}
140
167
141
168
# Assume the string is our username when the matching desired value is actually a credential
Write-Verbose-Message ('MATCH: PSCredential username match. Current state is {0} and desired state is {1}'-f$CurrentValues.$key,$DesiredValuesClean.$key.UserName)
171
+
Write-Verbose-Message ('MATCH: PSCredential username match. Current state is {0} and desired state is {1}'-f$CurrentValues.$key,$desiredValuesClean.$key.UserName)
145
172
continue
146
173
}
147
174
else
148
175
{
149
-
Write-Verbose-Message ('NOTMATCH: PSCredential username mismatch. Current state is {0} and desired state is {1}'-f$CurrentValues.$key,$DesiredValuesClean.$key.UserName)
176
+
Write-Verbose-Message ('NOTMATCH: PSCredential username mismatch. Current state is {0} and desired state is {1}'-f$CurrentValues.$key,$desiredValuesClean.$key.UserName)
150
177
$returnValue=$false
151
178
}
152
179
}
153
180
154
181
if (-not$TurnOffTypeChecking)
155
182
{
156
-
if (($desiredType.Name-ne'Unknown'-and$currentType.Name-ne'Unknown') -and
157
-
$desiredType.FullName-ne$currentType.FullName)
183
+
if (($desiredType.Name-ne'Unknown'-and$currentType.Name-ne'Unknown') -and
184
+
$desiredType.FullName-ne$currentType.FullName)
158
185
{
159
186
Write-Verbose-Message "NOTMATCH: Type mismatch for property '$key' Current state type is '$($currentType.Name)' and desired type is '$($desiredType.Name)'"
160
187
continue
161
188
}
162
189
}
163
190
164
-
if ($CurrentValues.$key-eq$DesiredValuesClean.$key-and-not$desiredType.IsArray)
191
+
if ($CurrentValues.$key-eq$desiredValuesClean.$key-and-not$desiredType.IsArray)
165
192
{
166
-
Write-Verbose-Message "MATCH: Value (type $($desiredType.Name)) for property '$key' does match. Current state is '$($CurrentValues.$key)' and desired state is '$($DesiredValuesClean.$key)'"
193
+
Write-Verbose-Message "MATCH: Value (type $($desiredType.Name)) for property '$key' does match. Current state is '$($CurrentValues.$key)' and desired state is '$($desiredValuesClean.$key)'"
167
194
continue
168
195
}
169
196
170
-
if ($DesiredValuesClean.GetType().Name -in'HashTable','PSBoundParametersDictionary')
197
+
if ($desiredValuesClean.GetType().Name -in'HashTable','PSBoundParametersDictionary')
Write-Verbose-Message "MATCH: Value (type $($desiredType.Name)) for property '$key' does match. Current state is '$($CurrentValues.$key)' and desired state is '$($DesiredValuesClean.$key)'"
208
+
Write-Verbose-Message "MATCH: Value (type $($desiredType.Name)) for property '$key' does match. Current state is '$($CurrentValues.$key)' and desired state is '$($desiredValuesClean.$key)'"
182
209
continue
183
210
}
184
211
@@ -187,13 +214,13 @@ function Test-DscParameterState
187
214
Write-Verbose"Comparing values in property '$key'"
188
215
if (-not$CurrentValues.ContainsKey($key) -or-not$CurrentValues.$key)
189
216
{
190
-
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does not match. Current state is '$($CurrentValues.$key)' and desired state is '$($DesiredValuesClean.$key)'"
217
+
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does not match. Current state is '$($CurrentValues.$key)' and desired state is '$($desiredValuesClean.$key)'"
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does have a different count. Current state count is '$($CurrentValues.$key.Count)' and desired state count is '$($DesiredValuesClean.$key.Count)'"
223
+
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does have a different count. Current state count is '$($CurrentValues.$key.Count)' and desired state count is '$($desiredValuesClean.$key.Count)'"
197
224
$returnValue=$false
198
225
continue
199
226
}
@@ -210,7 +237,9 @@ function Test-DscParameterState
210
237
}
211
238
else
212
239
{
213
-
$desiredType= [psobject]@{ Name='Unknown' }
240
+
$desiredType= [psobject]@{
241
+
Name='Unknown'
242
+
}
214
243
}
215
244
216
245
if ($null-ne$currentArrayValues[$i])
@@ -219,13 +248,15 @@ function Test-DscParameterState
219
248
}
220
249
else
221
250
{
222
-
$currentType= [psobject]@{ Name='Unknown' }
251
+
$currentType= [psobject]@{
252
+
Name='Unknown'
253
+
}
223
254
}
224
255
225
256
if (-not$TurnOffTypeChecking)
226
257
{
227
-
if (($desiredType.Name-ne'Unknown'-and$currentType.Name-ne'Unknown') -and
228
-
$desiredType.FullName-ne$currentType.FullName)
258
+
if (($desiredType.Name-ne'Unknown'-and$currentType.Name-ne'Unknown') -and
259
+
$desiredType.FullName-ne$currentType.FullName)
229
260
{
230
261
Write-Verbose-Message "`tNOTMATCH: Type mismatch for property '$key' Current state type of element [$i] is '$($currentType.Name)' and desired type is '$($desiredType.Name)'"
231
262
$returnValue=$false
@@ -248,10 +279,11 @@ function Test-DscParameterState
248
279
249
280
}
250
281
}
251
-
else {
252
-
if ($DesiredValuesClean.$key-ne$CurrentValues.$key)
282
+
else
283
+
{
284
+
if ($desiredValuesClean.$key-ne$CurrentValues.$key)
253
285
{
254
-
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does not match. Current state is '$($CurrentValues.$key)' and desired state is '$($DesiredValuesClean.$key)'"
286
+
Write-Verbose-Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does not match. Current state is '$($CurrentValues.$key)' and desired state is '$($desiredValuesClean.$key)'"
255
287
$returnValue=$false
256
288
}
257
289
@@ -262,24 +294,32 @@ function Test-DscParameterState
262
294
return$returnValue
263
295
}
264
296
297
+
<#
298
+
.SYNOPSIS
299
+
Tests of an object has a property
300
+
.PARAMETERObject
301
+
The object to test
302
+
.PARAMETERPropertyName
303
+
The property name
304
+
#>
265
305
functionTest-DSCObjectHasProperty
266
306
{
267
307
[CmdletBinding()]
268
308
[OutputType([bool])]
269
309
param
270
310
(
271
-
[Parameter(Mandatory)]
311
+
[Parameter(Mandatory=$true)]
272
312
[object]
273
313
$Object,
274
314
275
-
[Parameter(Mandatory)]
315
+
[Parameter(Mandatory=$true)]
276
316
[string]
277
317
$PropertyName
278
318
)
279
319
280
320
if ($Object.PSObject.Properties.Name-contains$PropertyName)
0 commit comments