Last active
July 2, 2022 17:16
-
-
Save johlju/9f42dc466416e0d1314d7a5ea007ab7b to your computer and use it in GitHub Desktop.
Problem returning property that is using class for a class-based DSC resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For some attempts | |
using module SqlServerDsc | |
$ConfigurationData = @{ | |
AllNodes = @( | |
@{ | |
NodeName = 'localhost' | |
PSDscAllowDomainUser = $true | |
PSDSCAllowPlainTextPassword = $true | |
} | |
) | |
} | |
Configuration Example | |
{ | |
Import-DscResource -ModuleName 'SqlServerDsc' | |
# For attempt 3 | |
$permisison1 = [DatabasePermission] @{ | |
State = 'Grant' | |
Permission = @('Connect', 'Update') | |
} | |
node localhost | |
{ | |
SimpleResource 'Grant-SQLTEST-sqluser' | |
{ | |
Ensure = 'Present' | |
Name = 'SQLTEST\sqluser' | |
DatabaseName = 'AdventureWorks2' | |
ServerName = 'localhost' | |
InstanceName = 'sql2017' | |
# Attempt 1 - throws compilation error | |
<# | |
Permission = [CimInstance[]] @( | |
( | |
New-CimInstance -ClientOnly -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName DatabasePermission -Property @{ | |
State = 'Grant' | |
Permission = @('Connect', 'Update') | |
} | |
) | |
) | |
#> | |
# Attempt 2 - throws compilation error | |
<# | |
Permission = [DatabasePermission[]] @( | |
[DatabasePermission] { | |
State = 'Grant' | |
Permission = @('Connect', 'Update') | |
} | |
) | |
#> | |
# Attempt 3 - throws compilation error | |
Permission = [DatabasePermission[]] @( | |
$permisison1 | |
) | |
} | |
} | |
} | |
Example -OutputPath c:\dsctemp -ConfigurationData $ConfigurationData |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Invoke-DscResource -ModuleName <modulename> -Name SimpleResource -Method Get -Property @{ | |
Ensure = 'Present' | |
ServerName = 'localhost' | |
InstanceName = 'sql2017' | |
DatabaseName = 'AdventureWorks2' | |
Name = 'SQLTEST\sqluser' | |
Permission = [CimInstance[]] @( | |
( | |
New-CimInstance -ClientOnly -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName DatabasePermission -Property @{ | |
State = 'Grant' | |
Permission = @('select') | |
} | |
) | |
( | |
New-CimInstance -ClientOnly -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName DatabasePermission -Property @{ | |
State = 'GrantWithGrant' | |
Permission = @('update') | |
} | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Ensure | |
{ | |
Present | |
Absent | |
} | |
class Reason | |
{ | |
[DscProperty()] | |
[System.String] | |
$Code | |
[DscProperty()] | |
[System.String] | |
$Phrase | |
} | |
class DatabasePermission | |
{ | |
[DscProperty(Key)] | |
[ValidateSet('Grant', 'GrantWithGrant', 'Deny')] | |
[System.String] | |
$State | |
[DscProperty(Mandatory)] | |
[System.String[]] | |
$Permission | |
} | |
[DscResource()] | |
class SimpleResource | |
{ | |
[DscProperty(Key)] | |
[System.String] | |
$InstanceName | |
[DscProperty(Key)] | |
[System.String] | |
$DatabaseName | |
[DscProperty(Key)] | |
[System.String] | |
$Name | |
[DscProperty()] | |
[System.String] | |
$ServerName | |
[DscProperty(Mandatory)] | |
[DatabasePermission[]] | |
$Permission | |
[DscProperty()] | |
[Ensure] | |
$Ensure = [Ensure]::Present | |
[DscProperty(NotConfigurable)] | |
[Reason[]] | |
$Reasons | |
[SimpleResource] Get() | |
{ | |
$dscResourceObject = [SimpleResource] @{ | |
InstanceName = 'SQL2017' | |
DatabaseName = 'MyDB' | |
Name = 'MyPrincipal' | |
ServerName = 'MyHost' | |
Ensure = 'Present' | |
Permission = [DatabasePermission[]] @( | |
[DatabasePermission] @{ | |
State = 'Grant' | |
Permission = @('CONNECT') | |
} | |
[DatabasePermission] @{ | |
State = 'Deny' | |
Permission = @('SELECT') | |
} | |
) | |
Reasons = [Reason[]] @( | |
[Reason] @{ | |
Code = '{0}:{0}:Ensure' -f $this.GetType() | |
Phrase = 'The property Ensure should be Present, but was Absent' | |
} | |
) | |
} | |
return $dscResourceObject | |
} | |
[System.Boolean] Test() | |
{ | |
return $true | |
} | |
[void] Set() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment