@@ -23,8 +23,7 @@ function Write-DscTrace {
23
23
if ($PSVersionTable.PSVersion.Major -gt 5 ) {
24
24
$m = Get-Module PSDesiredStateConfiguration - ListAvailable | Sort-Object - Descending | Select-Object - First 1
25
25
$PSDesiredStateConfiguration = Import-Module $m - Force - PassThru
26
- }
27
- else {
26
+ } else {
28
27
$env: PSModulePath = " $env: windir \System32\WindowsPowerShell\v1.0\Modules;$env: PSModulePath "
29
28
$PSDesiredStateConfiguration = Import-Module - Name ' PSDesiredStateConfiguration' - RequiredVersion ' 1.1' - Force - PassThru - ErrorAction stop - ErrorVariable $importModuleError
30
29
if (-not [string ]::IsNullOrEmpty($importModuleError )) {
@@ -63,16 +62,14 @@ function Invoke-DscCacheRefresh {
63
62
if ($cache.CacheSchemaVersion -ne $script :CurrentCacheSchemaVersion ) {
64
63
$refreshCache = $true
65
64
" Incompatible version of cache in file '" + $cache.CacheSchemaVersion + " ' (expected '" + $script :CurrentCacheSchemaVersion + " ')" | Write-DscTrace
66
- }
67
- else {
65
+ } else {
68
66
$dscResourceCacheEntries = $cache.ResourceCache
69
67
70
68
if ($dscResourceCacheEntries.Count -eq 0 ) {
71
69
# if there is nothing in the cache file - refresh cache
72
70
$refreshCache = $true
73
71
" Filtered DscResourceCache cache is empty" | Write-DscTrace
74
- }
75
- else {
72
+ } else {
76
73
" Checking cache for stale PSModulePath" | Write-DscTrace
77
74
78
75
$m = $env: PSModulePath -split [IO.Path ]::PathSeparator | ForEach-Object { Get-ChildItem - Directory - Path $_ - Depth 1 - ErrorAction Ignore }
@@ -104,8 +101,7 @@ function Invoke-DscCacheRefresh {
104
101
$namedModules.Add ($cacheEntry.DscResourceInfo.ModuleName )
105
102
break
106
103
}
107
- }
108
- else {
104
+ } else {
109
105
" Detected non-existent cache entry '$ ( $_.Name ) '" | Write-DscTrace
110
106
$namedModules.Add ($cacheEntry.DscResourceInfo.ModuleName )
111
107
break
@@ -123,8 +119,7 @@ function Invoke-DscCacheRefresh {
123
119
}
124
120
}
125
121
}
126
- }
127
- else {
122
+ } else {
128
123
" Cache file not found '$cacheFilePath '" | Write-DscTrace
129
124
$refreshCache = $true
130
125
}
@@ -159,8 +154,7 @@ function Invoke-DscCacheRefresh {
159
154
$filteredResources += $dscResources | Where-Object - Property ModuleName -NE $null | ForEach-Object { [System.String ]::Concat($_.ModuleName , ' /' , $_.Name ) }
160
155
# Exclude the one module that was passed in as a parameter
161
156
$existingDscResourceCacheEntries = @ ($cache.ResourceCache | Where-Object - Property Type -NotIn $filteredResources )
162
- }
163
- else {
157
+ } else {
164
158
# if no module is specified, get all resources
165
159
$DscResources = Get-DscResource
166
160
$Modules = Get-Module - ListAvailable
@@ -194,16 +188,14 @@ function Invoke-DscCacheRefresh {
194
188
$dscResource.PSObject.Properties | ForEach-Object - Process {
195
189
if ($null -ne $_.Value ) {
196
190
$DscResourceInfo .$ ($_.Name ) = $_.Value
197
- }
198
- else {
191
+ } else {
199
192
$DscResourceInfo .$ ($_.Name ) = ' '
200
193
}
201
194
}
202
195
203
196
if ($dscResource.ModuleName ) {
204
197
$moduleName = $dscResource.ModuleName
205
- }
206
- elseif ($binaryBuiltInModulePaths -contains $dscResource.ParentPath ) {
198
+ } elseif ($binaryBuiltInModulePaths -contains $dscResource.ParentPath ) {
207
199
$moduleName = ' PSDesiredStateConfiguration'
208
200
$DscResourceInfo.Module = ' PSDesiredStateConfiguration'
209
201
$DscResourceInfo.ModuleName = ' PSDesiredStateConfiguration'
@@ -212,8 +204,7 @@ function Invoke-DscCacheRefresh {
212
204
if ($PSVersionTable.PSVersion.Major -le 5 -and $DscResourceInfo.ImplementedAs -eq ' Binary' ) {
213
205
$DscResourceInfo.ImplementationDetail = ' Binary'
214
206
}
215
- }
216
- elseif ($binaryBuiltInModulePaths -notcontains $dscResource.ParentPath -and $null -ne $dscResource.ParentPath ) {
207
+ } elseif ($binaryBuiltInModulePaths -notcontains $dscResource.ParentPath -and $null -ne $dscResource.ParentPath ) {
217
208
# workaround: populate module name from parent path that is three levels up
218
209
$moduleName = Split-Path $dscResource.ParentPath | Split-Path | Split-Path - Leaf
219
210
$DscResourceInfo.Module = $moduleName
@@ -258,7 +249,7 @@ function Invoke-DscCacheRefresh {
258
249
259
250
[dscResourceCache ]$cache = [dscResourceCache ]::new()
260
251
$cache.ResourceCache = $dscResourceCacheEntries.ToArray ()
261
- $m = $env: PSModulePath -split [IO.Path ]::PathSeparator | % { Get-ChildItem - Directory - Path $_ - Depth 1 - ea SilentlyContinue }
252
+ $m = $env: PSModulePath -split [IO.Path ]::PathSeparator | ForEach-Object { Get-ChildItem - Directory - Path $_ - Depth 1 - ea SilentlyContinue }
262
253
$cache.PSModulePaths = $m.FullName
263
254
$cache.CacheSchemaVersion = $script :CurrentCacheSchemaVersion
264
255
@@ -365,12 +356,10 @@ function Invoke-DscOperation {
365
356
exit 1
366
357
}
367
358
$property .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
368
- }
369
- else {
359
+ } else {
370
360
$property .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
371
361
}
372
- }
373
- else {
362
+ } else {
374
363
$property [$_.Name ] = $_.Value
375
364
}
376
365
}
@@ -382,16 +371,14 @@ function Invoke-DscOperation {
382
371
383
372
if ($invokeResult.GetType ().Name -eq ' Hashtable' ) {
384
373
$invokeResult.keys | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
385
- }
386
- else {
374
+ } else {
387
375
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
388
376
$invokeResult.psobject.Properties.name | Where-Object { ' CimClass' , ' CimInstanceProperties' , ' CimSystemProperties' -notcontains $_ } | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
389
377
}
390
378
391
379
# set the properties of the OUTPUT object from the result of Get-TargetResource
392
380
$addToActualState.properties = $ResultProperties
393
- }
394
- catch {
381
+ } catch {
395
382
$_.Exception | Format-List * - Force | Out-String | Write-DscTrace - Operation Debug
396
383
if ($_.Exception.MessageId -eq ' DscResourceNotFound' ) {
397
384
Write-DscTrace - Operation Warn - Message ' For Windows PowerShell, DSC resources must be installed with scope AllUsers'
@@ -422,12 +409,10 @@ function Invoke-DscOperation {
422
409
exit 1
423
410
}
424
411
$dscResourceInstance .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
425
- }
426
- else {
412
+ } else {
427
413
$dscResourceInstance .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
428
414
}
429
- }
430
- else {
415
+ } else {
431
416
$dscResourceInstance .$ ($_.Name ) = $_.Value
432
417
}
433
418
}
@@ -440,8 +425,7 @@ function Invoke-DscOperation {
440
425
$ValidProperties | ForEach-Object {
441
426
if ($raw_obj .$_ -is [System.Enum ]) {
442
427
$Result [$_ ] = $raw_obj .$_.ToString ()
443
- }
444
- else {
428
+ } else {
445
429
$Result [$_ ] = $raw_obj .$_
446
430
}
447
431
}
@@ -463,8 +447,7 @@ function Invoke-DscOperation {
463
447
$ValidProperties | ForEach-Object {
464
448
if ($raw_obj .$_ -is [System.Enum ]) {
465
449
$Result_obj [$_ ] = $raw_obj .$_.ToString ()
466
- }
467
- else {
450
+ } else {
468
451
$Result_obj [$_ ] = $raw_obj .$_
469
452
}
470
453
}
@@ -473,8 +456,7 @@ function Invoke-DscOperation {
473
456
$addToActualState = $resultArray
474
457
}
475
458
}
476
- }
477
- catch {
459
+ } catch {
478
460
$_.Exception | Format-List * - Force | Out-String | Write-DscTrace - Operation Debug
479
461
if ($_.Exception.MessageId -eq ' DscResourceNotFound' ) {
480
462
Write-DscTrace - Operation Warn - Message ' For Windows PowerShell, DSC resources must be installed with scope AllUsers'
@@ -504,12 +486,10 @@ function Invoke-DscOperation {
504
486
exit 1
505
487
}
506
488
$property .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
507
- }
508
- else {
489
+ } else {
509
490
$property .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
510
491
}
511
- }
512
- else {
492
+ } else {
513
493
$property [$_.Name ] = $_.Value
514
494
}
515
495
}
@@ -520,16 +500,14 @@ function Invoke-DscOperation {
520
500
$invokeResult = Invoke-DscResource - Method $Operation - ModuleName $cachedDscResourceInfo.ModuleName - Name $cachedDscResourceInfo.Name - Property $property
521
501
if ($invokeResult.GetType ().Name -eq ' Hashtable' ) {
522
502
$invokeResult.keys | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
523
- }
524
- else {
503
+ } else {
525
504
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
526
505
$invokeResult.psobject.Properties.name | Where-Object { ' CimClass' , ' CimInstanceProperties' , ' CimSystemProperties' -notcontains $_ } | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
527
506
}
528
507
529
508
# set the properties of the OUTPUT object from the result of Get-TargetResource
530
509
$addToActualState.properties = $ResultProperties
531
- }
532
- catch {
510
+ } catch {
533
511
' Exception: ' + $_.Exception.Message | Write-DscTrace - Operation Error
534
512
exit 1
535
513
}
@@ -541,8 +519,7 @@ function Invoke-DscOperation {
541
519
}
542
520
543
521
return $addToActualState
544
- }
545
- else {
522
+ } else {
546
523
$dsJSON = $DesiredState | ConvertTo-Json - Depth 10
547
524
' Can not find type "' + $DesiredState.type + ' " for resource "' + $dsJSON + ' ". Please ensure that Get-DscResource returns this resource type.' | Write-DscTrace - Operation Error
548
525
exit 1
@@ -601,7 +578,7 @@ function GetClassBasedProperties {
601
578
return @ (' get' , ' set' , ' test' )
602
579
}
603
580
604
- $module = $filePath.Replace ( ' .psd1 ' , ' .psm1' )
581
+ $module = Get-ChildItem ( Split-Path $filePath - Parent) - Filter * .psm1 - File | Select-Object - First 1 - ExpandProperty FullName
605
582
606
583
$properties = [System.Collections.Generic.List [DscResourcePropertyInfo ]]::new()
607
584
0 commit comments