@@ -364,11 +364,16 @@ function Invoke-DscOperation {
364364 # morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
365365 $DesiredState.properties.psobject.properties | ForEach-Object - Begin { $property = @ {} } - Process {
366366 if ($_.Value -is [System.Management.Automation.PSCustomObject ]) {
367- if ($_.Name -like ' *Credential*' -and $_.Value.Username -and $_.Value.Password ) {
368- $property [$_.Name ] = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
367+ $validateProperty = $cachedDscResourceInfo.Properties | Where-Object - Property Name -EQ $_.Value
368+ if ($validateProperty.PropertyType -eq ' PSCredential' ) {
369+ if (-not $_.Value.Username -and -not $_.Value.Password ) {
370+ " Credential property '$ ( $_.Name ) ' requires both username and password input object" | Write-DscTrace - Operation Error
371+ exit 1
372+ }
373+ $dscResourceInstance .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
369374 }
370375 else {
371- $property [ $ _.Name ] = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
376+ $dscResourceInstance . $ ( $ _.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
372377 }
373378 }
374379 else {
@@ -409,7 +414,12 @@ function Invoke-DscOperation {
409414 $DesiredState.properties.psobject.properties | ForEach-Object - Process {
410415 # handle input objects by converting them to a hash table
411416 if ($_.Value -is [System.Management.Automation.PSCustomObject ]) {
412- if ($_.Name -like ' *Credential*' -and $_.Value.Username -and $_.Value.Password ) {
417+ $validateProperty = $cachedDscResourceInfo.Properties | Where-Object - Property Name -EQ $_.Value
418+ if ($validateProperty.PropertyType -eq ' PSCredential' ) {
419+ if (-not $_.Value.Username -and -not $_.Value.Password ) {
420+ " Credential property '$ ( $_.Name ) ' requires both username and password input object" | Write-DscTrace - Operation Error
421+ exit 1
422+ }
413423 $dscResourceInstance .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
414424 }
415425 else {
@@ -462,11 +472,16 @@ function Invoke-DscOperation {
462472 # morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
463473 $DesiredState.properties.psobject.properties | ForEach-Object - Begin { $property = @ {} } - Process {
464474 if ($_.Value -is [System.Management.Automation.PSCustomObject ]) {
465- if ($_.Name -Like ' *Credential*' -and $_.Value.Username -and $_.Value.Password ) {
466- $property [$_.Name ] = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
475+ $validateProperty = $cachedDscResourceInfo.Properties | Where-Object - Property Name -EQ $_.Value
476+ if ($validateProperty.PropertyType -eq ' PSCredential' ) {
477+ if (-not $_.Value.Username -and -not $_.Value.Password ) {
478+ " Credential property '$ ( $_.Name ) ' requires both username and password input object" | Write-DscTrace - Operation Error
479+ exit 1
480+ }
481+ $dscResourceInstance .$ ($_.Name ) = [System.Management.Automation.PSCredential ]::new($_.Value.Username , (ConvertTo-SecureString - AsPlainText $_.Value.Password - Force))
467482 }
468483 else {
469- $property [ $ _.Name ] = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
484+ $dscResourceInstance . $ ( $ _.Name ) = $_.Value.psobject.properties | ForEach-Object - Begin { $propertyHash = @ {} } - Process { $propertyHash [$_.Name ] = $_.Value } - End { $propertyHash }
470485 }
471486 }
472487 else {
0 commit comments