Skip to content

Commit 33d2f64

Browse files
authored
Merge pull request #1208 from Gijsreyn/gh-1207/main/fix-support-securestring
Add support for `[SecureString]` in PowerShell adapter
2 parents b4f0fb2 + 94382ea commit 33d2f64

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

adapters/powershell/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class TestClassResource : BaseTestClass
4040
[DscProperty()]
4141
[Ensure] $Ensure
4242

43+
[DscProperty()]
44+
[SecureString] $SecureStringProp
45+
4346
[string] $NonDscProperty # This property shouldn't be in results data
4447

4548
hidden

adapters/powershell/Tests/powershellgroup.resource.tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,11 @@ Describe 'PowerShell adapter resource tests' {
376376
$LASTEXITCODE | Should -Be 7
377377
Get-Content -Path $TestDrive/error.log | Should -Match 'Resource not found: TestClassResource/TestClassResource 0.0.2'
378378
}
379+
380+
It 'Can process SecureString property' {
381+
$r = '{"Name":"TestClassResource1","SecureStringProp":"MySecretValue"}' | dsc resource get -r 'TestClassResource/TestClassResource' -f -
382+
$LASTEXITCODE | Should -Be 0
383+
$res = $r | ConvertFrom-Json
384+
$res.actualState.SecureStringProp | Should -Not -BeNullOrEmpty
385+
}
379386
}

adapters/powershell/psDscAdapter/psDscAdapter.psm1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ function Invoke-DscOperation {
424424
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
425425
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
426426
# handle input objects by converting them to a hash table
427+
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
427428
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
428-
$validateProperty = $cachedDscResourceInfo.Properties | Where-Object -Property Name -EQ $_.Name
429-
if ($validateProperty -and $validateProperty.PropertyType -eq 'PSCredential') {
429+
if ($validateProperty -and $validateProperty.PropertyType -in @('PSCredential', 'System.Management.Automation.PSCredential')) {
430430
if (-not $_.Value.Username -or -not $_.Value.Password) {
431431
"Credential object '$($_.Name)' requires both 'username' and 'password' properties" | Write-DscTrace -Operation Error
432432
exit 1
@@ -438,7 +438,11 @@ function Invoke-DscOperation {
438438
}
439439
}
440440
else {
441-
$dscResourceInstance.$($_.Name) = $_.Value
441+
if ($validateProperty -and $validateProperty.PropertyType -in @('SecureString', 'System.Security.SecureString') -and -not [string]::IsNullOrEmpty($_.Value)) {
442+
$dscResourceInstance.$($_.Name) = ConvertTo-SecureString -AsPlainText $_.Value -Force
443+
} else {
444+
$dscResourceInstance.$($_.Name) = $_.Value
445+
}
442446
}
443447
}
444448
}

0 commit comments

Comments
 (0)