Skip to content

Commit ce01a1b

Browse files
committed
cleanup module imports
1 parent ad75cfa commit ce01a1b

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
if ($PSVersionTable.PSVersion.Major -gt 5) {
99
$PSDesiredStateConfiguration = Import-Module -Path '../PSDesiredStateConfiguration/2.0.7/PSDesiredStateConfiguration.psd1' -Force -PassThru
1010
}
11+
else {
12+
$env:PSModulePath += ";$env:windir\System32\WindowsPowerShell\v1.0\Modules"
13+
$PSDesiredStateConfiguration = Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion '1.1' -Force -PassThru -ErrorAction stop -ErrorVariable $importModuleError
14+
if (-not [string]::IsNullOrEmpty($importModuleError)) {
15+
$trace = @{'Debug' = 'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError } | ConvertTo-Json -Compress
16+
$host.ui.WriteErrorLine($trace)
17+
}
18+
}
1119

1220
<# public function Invoke-DscCacheRefresh
1321
.SYNOPSIS
@@ -28,17 +36,7 @@ function Invoke-DscCacheRefresh {
2836
[Object[]]
2937
$Module
3038
)
31-
# for the WindowsPowerShell adapter, always use the version of PSDesiredStateConfiguration that ships in Windows
32-
if ($PSVersionTable.PSVersion.Major -le 5) {
33-
$psdscWindowsPath = "$env:windir\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1"
34-
Import-Module $psdscWindowsPath -Force -ErrorAction stop -ErrorVariable $importModuleError
35-
if (-not [string]::IsNullOrEmpty($importModuleError)) {
36-
$trace = @{'Debug' = 'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError } | ConvertTo-Json -Compress
37-
$host.ui.WriteErrorLine($trace)
38-
}
39-
$DSCVersion = [version]'1.1.0'
40-
}
41-
39+
4240
# cache the results of Get-DscResource
4341
[dscResourceCache[]]$dscResourceCache = @()
4442

@@ -72,7 +70,7 @@ function Invoke-DscCacheRefresh {
7270
}
7371

7472
# we can't run this check in PSDesiredStateConfiguration 1.1 because the property doesn't exist
75-
if ( $DSCVersion -ge [version]'2.0.0' ) {
73+
if ( $PSVersionTable.PSVersion.Major -le 5 ) {
7674
# only support known dscResourceType
7775
if ([dscResourceType].GetEnumNames() -notcontains $dscResource.ImplementationDetail) {
7876
$trace = @{'Debug' = 'WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail } | ConvertTo-Json -Compress
@@ -90,7 +88,7 @@ function Invoke-DscCacheRefresh {
9088
)
9189
$DscResourceInfo = [DscResourceInfo]::new()
9290
$dscResource.PSObject.Properties | ForEach-Object -Process {
93-
if ($null -eq $_.Value) {$_.Value = ''}
91+
if ($null -eq $_.Value) { $_.Value = '' }
9492
$DscResourceInfo.$($_.Name) = $_.Value
9593
}
9694
if ($dscResource.ModuleName) {
@@ -182,16 +180,6 @@ function Get-ActualState {
182180
[dscResourceCache[]]$dscResourceCache
183181
)
184182

185-
# for the WindowsPowerShell adapter, always use the version of PSDesiredStateConfiguration that ships in Windows
186-
if ($PSVersionTable.PSVersion.Major -le 5) {
187-
$psdscWindowsPath = "$env:windir\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1"
188-
Import-Module $psdscWindowsPath -Force -ErrorAction stop -ErrorVariable $importModuleError
189-
if (-not [string]::IsNullOrEmpty($importModuleError)) {
190-
$trace = @{'Debug' = 'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError } | ConvertTo-Json -Compress
191-
$host.ui.WriteErrorLine($trace)
192-
}
193-
}
194-
195183
$osVersion = [System.Environment]::OSVersion.VersionString
196184
$trace = @{'Debug' = 'OS version: ' + $osVersion } | ConvertTo-Json -Compress
197185
$host.ui.WriteErrorLine($trace)
@@ -298,11 +286,9 @@ function Get-ActualState {
298286

299287
# using the cmdlet from PSDesiredStateConfiguration module in Windows
300288
try {
301-
$env:PSModulePath += ";$env:windir\System32\WindowsPowerShell\v1.0\Modules"
302-
$PSDesiredStateConfiguration = Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion '1.1' -Force -PassThru
303-
$getResult = $PSDesiredStateConfiguration.invoke({param($Name, $Property) Invoke-DscResource -Name $Name -Method Get -ModuleName @{ModuleName = 'PSDesiredStateConfiguration'; ModuleVersion = '1.1'} -Property $Property }, $cachedDscResourceInfo.Name, $property )
289+
$getResult = $PSDesiredStateConfiguration.invoke({ param($Name, $Property) Invoke-DscResource -Name $Name -Method Get -ModuleName @{ModuleName = 'PSDesiredStateConfiguration'; ModuleVersion = '1.1' } -Property $Property }, $cachedDscResourceInfo.Name, $property )
304290

305-
$trace = @{'Debug' = 'TEMP output: ' + $($getResult | convertto-json -depth 10 -Compress) } | ConvertTo-Json -Compress
291+
$trace = @{'Debug' = 'TEMP output: ' + $($getResult | ConvertTo-Json -Depth 10 -Compress) } | ConvertTo-Json -Compress
306292
$host.ui.WriteErrorLine($trace)
307293

308294
# only return DSC properties from the Cim instance

0 commit comments

Comments
 (0)