Skip to content

Commit 395980a

Browse files
authored
Merge pull request #39 from raandree/master
Cleanup according to DscResource.Common
2 parents 1a0b322 + eb76bee commit 395980a

File tree

6 files changed

+61
-541
lines changed

6 files changed

+61
-541
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66
## [Unreleased]
77

88
- Renamed 'Test-DscParameterState' to 'Test-DscParameterState2' for a conflict with 'DscResource.Common'.
9+
- Removing functions provided by 'DscResource.Common'
10+
- Making property 'RoleDefinitions' non-mandatory
11+
- Replacing 'New-PSRoleCapabilityFile' by writing the file directly
12+
- Making 'ConvertTo-Expression' visible as it is required also from the outside
13+
- Renamed variable '$parameters' into '$desiredState'
914

1015
### Added
1116

source/DSCClassResources/JeaRoleCapabilities/JeaRoleCapabilities.psm1

+21-20
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ enum Ensure
66

77
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'
88

9-
# Import the JeaDsc Common Module
10-
Import-Module -Name (Join-Path -Path $modulePath `
11-
-ChildPath (Join-Path -Path 'JeaDsc.Common' `
12-
-ChildPath 'JeaDsc.Common.psm1'))
9+
Import-Module -Name (Join-Path -Path $modulePath -ChildPath DscResource.Common)
10+
Import-Module -Name (Join-Path -Path $modulePath -ChildPath (Join-Path -Path JeaDsc.Common -ChildPath JeaDsc.Common.psm1))
1311

14-
Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common')
12+
$script:localizedData = Get-LocalizedData -DefaultUICulture en-US
1513

1614
[DscResource()]
1715
class JeaRoleCapabilities
@@ -176,20 +174,20 @@ class JeaRoleCapabilities
176174

177175
if ($this.Ensure -eq [Ensure]::Present)
178176
{
179-
$parameters = Convert-ObjectToHashtable -Object $this
177+
$desiredState = Convert-ObjectToHashtable -Object $this
180178

181-
foreach ($parameter in $parameters.Keys.Where( { $parameters[$_] -match '@{' }))
179+
foreach ($parameter in $desiredState.Keys.Where( { $desiredState[$_] -match '@{' }))
182180
{
183-
$parameters[$parameter] = Convert-StringToObject -InputString $parameters[$parameter]
181+
$desiredState[$parameter] = Convert-StringToObject -InputString $desiredState[$parameter]
184182
}
185183

186-
$parameters = Sync-Parameter -Command (Get-Command -Name New-PSRoleCapabilityFile) -Parameters $parameters
184+
$desiredState = Sync-Parameter -Command (Get-Command -Name New-PSRoleCapabilityFile) -Parameters $desiredState
187185

188-
if ($parameters.ContainsKey('FunctionDefinitions'))
186+
if ($desiredState.ContainsKey('FunctionDefinitions'))
189187
{
190-
foreach ($functionDefinitionName in $Parameters['FunctionDefinitions'].Name)
188+
foreach ($functionDefinitionName in $desiredState['FunctionDefinitions'].Name)
191189
{
192-
if ($functionDefinitionName -notin $Parameters['VisibleFunctions'])
190+
if ($functionDefinitionName -notin $desiredState['VisibleFunctions'])
193191
{
194192
Write-Verbose -"Function defined but not visible to Role Configuration: $functionDefinitionName"
195193
Write-Error "Function defined but not visible to Role Configuration: $functionDefinitionName"
@@ -200,9 +198,13 @@ class JeaRoleCapabilities
200198

201199
if (-not $invalidConfiguration)
202200
{
203-
$parentPath = Split-Path -Path $parameters.Path -Parent
201+
$parentPath = Split-Path -Path $desiredState.Path -Parent
204202
mkdir -Path $parentPath -Force
205-
New-PSRoleCapabilityFile @parameters
203+
204+
$fPath = $desiredState.Path
205+
$desiredState.Remove('Path')
206+
$content = $desiredState | ConvertTo-Expression
207+
$content | Set-Content -Path $fPath -Force
206208
}
207209
}
208210
elseif ($this.Ensure -eq [Ensure]::Absent -and (Test-Path -Path $this.Path))
@@ -227,25 +229,24 @@ class JeaRoleCapabilities
227229
{
228230

229231
$currentState = Convert-ObjectToHashtable -Object $this.Get()
230-
$parameters = Convert-ObjectToHashtable -Object $this
232+
$desiredState = Convert-ObjectToHashtable -Object $this
231233

232234
$cmdlet = Get-Command -Name New-PSRoleCapabilityFile
233-
$parameters = Sync-Parameter -Command $cmdlet -Parameters $parameters
235+
$desiredState = Sync-Parameter -Command $cmdlet -Parameters $desiredState
234236
$currentState = Sync-Parameter -Command $cmdlet -Parameters $currentState
235237
$propertiesAsObject = $cmdlet.Parameters.Keys |
236-
Where-Object { $_ -in $parameters.Keys } |
238+
Where-Object { $_ -in $desiredState.Keys } |
237239
Where-Object { $cmdlet.Parameters.$_.ParameterType.FullName -in 'System.Collections.IDictionary', 'System.Collections.Hashtable', 'System.Collections.IDictionary[]', 'System.Object[]' }
238240
foreach ($p in $propertiesAsObject)
239241
{
240242
if ($cmdlet.Parameters.$p.ParameterType.FullName -in 'System.Collections.Hashtable', 'System.Collections.IDictionary', 'System.Collections.IDictionary[]', 'System.Object[]')
241243
{
242-
$parameters."$($p)" = $parameters."$($p)" | Convert-StringToObject
244+
$desiredState."$($p)" = $desiredState."$($p)" | Convert-StringToObject
243245
$currentState."$($p)" = $currentState."$($p)" | Convert-StringToObject
244-
245246
}
246247
}
247248

248-
$compare = Test-DscParameterState2 -CurrentValues $currentState -DesiredValues $Parameters -SortArrayValues -TurnOffTypeChecking -ReverseCheck
249+
$compare = Test-DscParameterState -CurrentValues $currentState -DesiredValues $desiredState -SortArrayValues -TurnOffTypeChecking -ReverseCheck
249250

250251
return $compare
251252
}

source/DSCClassResources/JeaSessionConfiguration/JeaSessionConfiguration.psm1

+16-20
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ enum Ensure
66

77
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath Modules
88

9-
# Import the JeaDsc Common Module
10-
Import-Module -Name (Join-Path -Path $modulePath `
11-
-ChildPath (Join-Path -Path JeaDsc.Common `
12-
-ChildPath JeaDsc.Common.psm1))
13-
149
Import-Module -Name (Join-Path -Path $modulePath -ChildPath DscResource.Common)
10+
Import-Module -Name (Join-Path -Path $modulePath -ChildPath (Join-Path -Path JeaDsc.Common -ChildPath JeaDsc.Common.psm1))
1511

1612
$script:localizedData = Get-LocalizedData -DefaultUICulture en-US
1713

@@ -26,11 +22,11 @@ class JeaSessionConfiguration
2622
[DscProperty(Key)]
2723
[string] $Name = 'Microsoft.PowerShell'
2824

29-
## The mandatory role definition map to be used for the endpoint. This
25+
## The role definition map to be used for the endpoint. This
3026
## should be a string that represents the Hashtable used for the RoleDefinitions
3127
## property in New-PSSessionConfigurationFile, such as:
3228
## RoleDefinitions = '@{ Everyone = @{ RoleCapabilities = "BaseJeaCapabilities" } }'
33-
[Dscproperty(Mandatory)]
29+
[Dscproperty()]
3430
[string] $RoleDefinitions
3531

3632
## run the endpoint under a Virtual Account
@@ -177,14 +173,14 @@ class JeaSessionConfiguration
177173

178174
$psscPath = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName() + ".pssc")
179175
Write-Verbose "Storing PSSessionConfigurationFile in file '$psscPath'"
180-
$parameters = Convert-ObjectToHashtable -Object $this
181-
$parameters.Add('Path', $psscPath)
176+
$desiredState = Convert-ObjectToHashtable -Object $this
177+
$desiredState.Add('Path', $psscPath)
182178

183179
if ($this.Ensure -eq [Ensure]::Present)
184180
{
185-
foreach ($parameter in $parameters.Keys.Where( { $parameters[$_] -match '@{' }))
181+
foreach ($parameter in $desiredState.Keys.Where( { $desiredState[$_] -match '@{' }))
186182
{
187-
$parameters[$parameter] = Convert-StringToObject -InputString $parameters[$parameter]
183+
$desiredState[$parameter] = Convert-StringToObject -InputString $desiredState[$parameter]
188184
}
189185
}
190186

@@ -211,8 +207,8 @@ class JeaSessionConfiguration
211207
{
212208
## Create the configuration file
213209
#New-PSSessionConfigurationFile @configurationFileArguments
214-
$parameters = Sync-Parameter -Command (Get-Command -Name New-PSSessionConfigurationFile) -Parameters $parameters
215-
New-PSSessionConfigurationFile @parameters
210+
$desiredState = Sync-Parameter -Command (Get-Command -Name New-PSSessionConfigurationFile) -Parameters $desiredState
211+
New-PSSessionConfigurationFile @desiredState
216212

217213
## Register the configuration file
218214
$this.RegisterPSSessionConfiguration($this.Name, $psscPath, $this.HungRegistrationTimeout)
@@ -235,12 +231,12 @@ class JeaSessionConfiguration
235231
[bool] Test()
236232
{
237233
$currentState = Convert-ObjectToHashtable -Object $this.Get()
238-
$parameters = Convert-ObjectToHashtable -Object $this
234+
$desiredState = Convert-ObjectToHashtable -Object $this
239235

240236
# short-circuit if the resource is not present and is not supposed to be present
241-
if ($currentState.Ensure -ne $parameters.Ensure)
237+
if ($currentState.Ensure -ne $desiredState.Ensure)
242238
{
243-
Write-Verbose "Desired state of session configuration named '$($currentState.Name)' is '$($parameters.Ensure)', current state is '$($currentState.Ensure)' "
239+
Write-Verbose "Desired state of session configuration named '$($currentState.Name)' is '$($desiredState.Ensure)', current state is '$($currentState.Ensure)' "
244240
return $false
245241
}
246242
if ($this.Ensure -eq [Ensure]::Absent)
@@ -255,22 +251,22 @@ class JeaSessionConfiguration
255251
}
256252

257253
$cmdlet = Get-Command -Name New-PSSessionConfigurationFile
258-
$parameters = Sync-Parameter -Command $cmdlet -Parameters $parameters
254+
$desiredState = Sync-Parameter -Command $cmdlet -Parameters $desiredState
259255
$currentState = Sync-Parameter -Command $cmdlet -Parameters $currentState
260256
$propertiesAsObject = $cmdlet.Parameters.Keys |
261-
Where-Object { $_ -in $parameters.Keys } |
257+
Where-Object { $_ -in $desiredState.Keys } |
262258
Where-Object { $cmdlet.Parameters.$_.ParameterType.FullName -in 'System.Collections.IDictionary', 'System.Collections.Hashtable', 'System.Collections.IDictionary[]', 'System.Object[]' }
263259
foreach ($p in $propertiesAsObject)
264260
{
265261
if ($cmdlet.Parameters.$p.ParameterType.FullName -in 'System.Collections.Hashtable', 'System.Collections.IDictionary', 'System.Collections.IDictionary[]', 'System.Object[]')
266262
{
267-
$parameters."$($p)" = $parameters."$($p)" | Convert-StringToObject
263+
$desiredState."$($p)" = $desiredState."$($p)" | Convert-StringToObject
268264
$currentState."$($p)" = $currentState."$($p)" | Convert-StringToObject
269265

270266
}
271267
}
272268

273-
$compare = Test-DscParameterState2 -CurrentValues $currentState -DesiredValues $parameters -TurnOffTypeChecking -SortArrayValues -ReverseCheck
269+
$compare = Test-DscParameterState -CurrentValues $currentState -DesiredValues $desiredState -TurnOffTypeChecking -SortArrayValues -ReverseCheck
274270

275271
return $compare
276272
}

source/JeaDsc.psd1

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
'DSCClassResources\JeaRoleCapabilities\JeaRoleCapabilities.psd1'
2222
)
2323

24-
FunctionsToExport = @()
24+
FunctionsToExport = @(
25+
'ConvertTo-Expression'
26+
)
2527

2628
CmdletsToExport = @()
2729

0 commit comments

Comments
 (0)