@@ -23,8 +23,7 @@ function Write-DscTrace {
2323if ($PSVersionTable.PSVersion.Major -gt 5 ) {
2424 $m = Get-Module PSDesiredStateConfiguration - ListAvailable | Sort-Object - Descending | Select-Object - First 1
2525 $PSDesiredStateConfiguration = Import-Module $m - Force - PassThru
26- }
27- else {
26+ } else {
2827 $env: PSModulePath = " $env: windir \System32\WindowsPowerShell\v1.0\Modules;$env: PSModulePath "
2928 $PSDesiredStateConfiguration = Import-Module - Name ' PSDesiredStateConfiguration' - RequiredVersion ' 1.1' - Force - PassThru - ErrorAction stop - ErrorVariable $importModuleError
3029 if (-not [string ]::IsNullOrEmpty($importModuleError )) {
@@ -63,16 +62,14 @@ function Invoke-DscCacheRefresh {
6362 if ($cache.CacheSchemaVersion -ne $script :CurrentCacheSchemaVersion ) {
6463 $refreshCache = $true
6564 " Incompatible version of cache in file '" + $cache.CacheSchemaVersion + " ' (expected '" + $script :CurrentCacheSchemaVersion + " ')" | Write-DscTrace
66- }
67- else {
65+ } else {
6866 $dscResourceCacheEntries = $cache.ResourceCache
6967
7068 if ($dscResourceCacheEntries.Count -eq 0 ) {
7169 # if there is nothing in the cache file - refresh cache
7270 $refreshCache = $true
7371 " Filtered DscResourceCache cache is empty" | Write-DscTrace
74- }
75- else {
72+ } else {
7673 " Checking cache for stale PSModulePath" | Write-DscTrace
7774
7875 $m = $env: PSModulePath -split [IO.Path ]::PathSeparator | ForEach-Object { Get-ChildItem - Directory - Path $_ - Depth 1 - ErrorAction Ignore }
@@ -104,8 +101,7 @@ function Invoke-DscCacheRefresh {
104101 $namedModules.Add ($cacheEntry.DscResourceInfo.ModuleName )
105102 break
106103 }
107- }
108- else {
104+ } else {
109105 " Detected non-existent cache entry '$ ( $_.Name ) '" | Write-DscTrace
110106 $namedModules.Add ($cacheEntry.DscResourceInfo.ModuleName )
111107 break
@@ -123,8 +119,7 @@ function Invoke-DscCacheRefresh {
123119 }
124120 }
125121 }
126- }
127- else {
122+ } else {
128123 " Cache file not found '$cacheFilePath '" | Write-DscTrace
129124 $refreshCache = $true
130125 }
@@ -159,8 +154,7 @@ function Invoke-DscCacheRefresh {
159154 $filteredResources += $dscResources | Where-Object - Property ModuleName -NE $null | ForEach-Object { [System.String ]::Concat($_.ModuleName , ' /' , $_.Name ) }
160155 # Exclude the one module that was passed in as a parameter
161156 $existingDscResourceCacheEntries = @ ($cache.ResourceCache | Where-Object - Property Type -NotIn $filteredResources )
162- }
163- else {
157+ } else {
164158 # if no module is specified, get all resources
165159 $DscResources = Get-DscResource
166160 $Modules = Get-Module - ListAvailable
@@ -194,16 +188,14 @@ function Invoke-DscCacheRefresh {
194188 $dscResource.PSObject.Properties | ForEach-Object - Process {
195189 if ($null -ne $_.Value ) {
196190 $DscResourceInfo .$ ($_.Name ) = $_.Value
197- }
198- else {
191+ } else {
199192 $DscResourceInfo .$ ($_.Name ) = ' '
200193 }
201194 }
202195
203196 if ($dscResource.ModuleName ) {
204197 $moduleName = $dscResource.ModuleName
205- }
206- elseif ($binaryBuiltInModulePaths -contains $dscResource.ParentPath ) {
198+ } elseif ($binaryBuiltInModulePaths -contains $dscResource.ParentPath ) {
207199 $moduleName = ' PSDesiredStateConfiguration'
208200 $DscResourceInfo.Module = ' PSDesiredStateConfiguration'
209201 $DscResourceInfo.ModuleName = ' PSDesiredStateConfiguration'
@@ -212,8 +204,7 @@ function Invoke-DscCacheRefresh {
212204 if ($PSVersionTable.PSVersion.Major -le 5 -and $DscResourceInfo.ImplementedAs -eq ' Binary' ) {
213205 $DscResourceInfo.ImplementationDetail = ' Binary'
214206 }
215- }
216- elseif ($binaryBuiltInModulePaths -notcontains $dscResource.ParentPath -and $null -ne $dscResource.ParentPath ) {
207+ } elseif ($binaryBuiltInModulePaths -notcontains $dscResource.ParentPath -and $null -ne $dscResource.ParentPath ) {
217208 # workaround: populate module name from parent path that is three levels up
218209 $moduleName = Split-Path $dscResource.ParentPath | Split-Path | Split-Path - Leaf
219210 $DscResourceInfo.Module = $moduleName
@@ -258,7 +249,7 @@ function Invoke-DscCacheRefresh {
258249
259250 [dscResourceCache ]$cache = [dscResourceCache ]::new()
260251 $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 }
262253 $cache.PSModulePaths = $m.FullName
263254 $cache.CacheSchemaVersion = $script :CurrentCacheSchemaVersion
264255
@@ -365,12 +356,10 @@ function Invoke-DscOperation {
365356 exit 1
366357 }
367358 $property .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
368- }
369- else {
359+ } else {
370360 $property .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
371361 }
372- }
373- else {
362+ } else {
374363 $property [$_.Name ] = $_.Value
375364 }
376365 }
@@ -382,16 +371,14 @@ function Invoke-DscOperation {
382371
383372 if ($invokeResult.GetType ().Name -eq ' Hashtable' ) {
384373 $invokeResult.keys | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
385- }
386- else {
374+ } else {
387375 # the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
388376 $invokeResult.psobject.Properties.name | Where-Object { ' CimClass' , ' CimInstanceProperties' , ' CimSystemProperties' -notcontains $_ } | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
389377 }
390378
391379 # set the properties of the OUTPUT object from the result of Get-TargetResource
392380 $addToActualState.properties = $ResultProperties
393- }
394- catch {
381+ } catch {
395382 $_.Exception | Format-List * - Force | Out-String | Write-DscTrace - Operation Debug
396383 if ($_.Exception.MessageId -eq ' DscResourceNotFound' ) {
397384 Write-DscTrace - Operation Warn - Message ' For Windows PowerShell, DSC resources must be installed with scope AllUsers'
@@ -422,12 +409,10 @@ function Invoke-DscOperation {
422409 exit 1
423410 }
424411 $dscResourceInstance .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
425- }
426- else {
412+ } else {
427413 $dscResourceInstance .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
428414 }
429- }
430- else {
415+ } else {
431416 $dscResourceInstance .$ ($_.Name ) = $_.Value
432417 }
433418 }
@@ -440,8 +425,7 @@ function Invoke-DscOperation {
440425 $ValidProperties | ForEach-Object {
441426 if ($raw_obj .$_ -is [System.Enum ]) {
442427 $Result [$_ ] = $raw_obj .$_.ToString ()
443- }
444- else {
428+ } else {
445429 $Result [$_ ] = $raw_obj .$_
446430 }
447431 }
@@ -463,8 +447,7 @@ function Invoke-DscOperation {
463447 $ValidProperties | ForEach-Object {
464448 if ($raw_obj .$_ -is [System.Enum ]) {
465449 $Result_obj [$_ ] = $raw_obj .$_.ToString ()
466- }
467- else {
450+ } else {
468451 $Result_obj [$_ ] = $raw_obj .$_
469452 }
470453 }
@@ -473,8 +456,7 @@ function Invoke-DscOperation {
473456 $addToActualState = $resultArray
474457 }
475458 }
476- }
477- catch {
459+ } catch {
478460 $_.Exception | Format-List * - Force | Out-String | Write-DscTrace - Operation Debug
479461 if ($_.Exception.MessageId -eq ' DscResourceNotFound' ) {
480462 Write-DscTrace - Operation Warn - Message ' For Windows PowerShell, DSC resources must be installed with scope AllUsers'
@@ -504,12 +486,10 @@ function Invoke-DscOperation {
504486 exit 1
505487 }
506488 $property .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
507- }
508- else {
489+ } else {
509490 $property .$ ($_.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
510491 }
511- }
512- else {
492+ } else {
513493 $property [$_.Name ] = $_.Value
514494 }
515495 }
@@ -520,16 +500,14 @@ function Invoke-DscOperation {
520500 $invokeResult = Invoke-DscResource - Method $Operation - ModuleName $cachedDscResourceInfo.ModuleName - Name $cachedDscResourceInfo.Name - Property $property
521501 if ($invokeResult.GetType ().Name -eq ' Hashtable' ) {
522502 $invokeResult.keys | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
523- }
524- else {
503+ } else {
525504 # the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
526505 $invokeResult.psobject.Properties.name | Where-Object { ' CimClass' , ' CimInstanceProperties' , ' CimSystemProperties' -notcontains $_ } | ForEach-Object - Begin { $ResultProperties = @ {} } - Process { $ResultProperties [$_ ] = $invokeResult .$_ }
527506 }
528507
529508 # set the properties of the OUTPUT object from the result of Get-TargetResource
530509 $addToActualState.properties = $ResultProperties
531- }
532- catch {
510+ } catch {
533511 ' Exception: ' + $_.Exception.Message | Write-DscTrace - Operation Error
534512 exit 1
535513 }
@@ -541,8 +519,7 @@ function Invoke-DscOperation {
541519 }
542520
543521 return $addToActualState
544- }
545- else {
522+ } else {
546523 $dsJSON = $DesiredState | ConvertTo-Json - Depth 10
547524 ' Can not find type "' + $DesiredState.type + ' " for resource "' + $dsJSON + ' ". Please ensure that Get-DscResource returns this resource type.' | Write-DscTrace - Operation Error
548525 exit 1
@@ -601,7 +578,7 @@ function GetClassBasedProperties {
601578 return @ (' get' , ' set' , ' test' )
602579 }
603580
604- $module = $filePath.Replace ( ' .psd1 ' , ' .psm1' )
581+ $module = Get-ChildItem ( Split-Path $filePath - Parent) - Filter * .psm1 - File | Select-Object - First 1 - ExpandProperty FullName
605582
606583 $properties = [System.Collections.Generic.List [DscResourcePropertyInfo ]]::new()
607584
0 commit comments