-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public commands for class-based resource (#96)
- Added public command `Get-DscProperty` that returns a hashtable of available properties for a class-based resource. See comment-based help for more information. - Added public command `Test-DscProperty` that returns a true or false whether a property exist in a class-based resource. See comment-based help for more information. - Added private function `Test-DscPropertyIsAssigned` that returns a true or false whether a property in a class-based resource has a non-null value.
- Loading branch information
Showing
8 changed files
with
1,569 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<# | ||
.SYNOPSIS | ||
Tests whether the class-based resource property is assigned a non-null value. | ||
.DESCRIPTION | ||
Tests whether the class-based resource property is assigned a non-null value. | ||
.PARAMETER InputObject | ||
Specifies the object that contain the property. | ||
.PARAMETER Name | ||
Specifies the name of the property. | ||
.EXAMPLE | ||
Test-DscPropertyIsAssigned -InputObject $this -Name 'MyDscProperty' | ||
Returns $true or $false whether the property is assigned or not. | ||
.OUTPUTS | ||
[System.Boolean] | ||
.NOTES | ||
This command only works with nullable data types, if using a non-nullable | ||
type make sure to make it nullable, e.g. [nullable[System.Int32]]. | ||
#> | ||
function Test-DscPropertyIsAssigned | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[PSObject] | ||
$InputObject, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$Name | ||
) | ||
|
||
begin | ||
{ | ||
$isAssigned = $false | ||
} | ||
|
||
process | ||
{ | ||
$isAssigned = -not ($null -eq $InputObject.$Name) | ||
} | ||
|
||
end | ||
{ | ||
return $isAssigned | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
|
||
<# | ||
.SYNOPSIS | ||
Returns DSC resource properties that is part of a class-based DSC resource. | ||
.DESCRIPTION | ||
Returns DSC resource properties that is part of a class-based DSC resource. | ||
The properties can be filtered using name, attribute, or if it has been | ||
assigned a value. | ||
.PARAMETER InputObject | ||
The object that contain one or more key properties. | ||
.PARAMETER Name | ||
Specifies one or more property names to return. If left out all properties | ||
are returned. | ||
.PARAMETER ExcludeName | ||
Specifies one or more property names to exclude. | ||
.PARAMETER Attribute | ||
Specifies one or more property attributes to return. If left out all property | ||
types are returned. | ||
.PARAMETER HasValue | ||
Specifies to return only properties that has been assigned a non-null value. | ||
If left out all properties are returned regardless if there is a value | ||
assigned or not. | ||
.EXAMPLE | ||
Get-DscProperty -InputObject $this | ||
Returns all DSC resource properties of the DSC resource. | ||
.EXAMPLE | ||
$this | Get-DscProperty | ||
Returns all DSC resource properties of the DSC resource. | ||
.EXAMPLE | ||
Get-DscProperty -InputObject $this -Name @('MyProperty1', 'MyProperty2') | ||
Returns the DSC resource properties with the specified names. | ||
.EXAMPLE | ||
Get-DscProperty -InputObject $this -Attribute @('Mandatory', 'Optional') | ||
Returns the DSC resource properties that has the specified attributes. | ||
.EXAMPLE | ||
Get-DscProperty -InputObject $this -Attribute @('Optional') -HasValue | ||
Returns the DSC resource properties that has the specified attributes and | ||
has a non-null value assigned. | ||
.OUTPUTS | ||
[System.Collections.Hashtable] | ||
.NOTES | ||
This command only works with nullable data types, if using a non-nullable | ||
type make sure to make it nullable, e.g. [Nullable[System.Int32]]. | ||
#> | ||
function Get-DscProperty | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Collections.Hashtable])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[PSObject] | ||
$InputObject, | ||
|
||
[Parameter()] | ||
[System.String[]] | ||
$Name, | ||
|
||
[Parameter()] | ||
[System.String[]] | ||
$ExcludeName, | ||
|
||
[Parameter()] | ||
[ValidateSet('Key', 'Mandatory', 'NotConfigurable', 'Optional')] | ||
[Alias('Type')] | ||
[System.String[]] | ||
$Attribute, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$HasValue | ||
) | ||
|
||
process | ||
{ | ||
$property = $InputObject.PSObject.Properties.Name | | ||
Where-Object -FilterScript { | ||
<# | ||
Return all properties if $Name is not assigned, or if assigned | ||
just those properties. | ||
#> | ||
(-not $Name -or $_ -in $Name) -and | ||
|
||
<# | ||
Return all properties if $ExcludeName is not assigned. Skip | ||
property if it is included in $ExcludeName. | ||
#> | ||
(-not $ExcludeName -or ($_ -notin $ExcludeName)) -and | ||
|
||
# Only return the property if it is a DSC property. | ||
$InputObject.GetType().GetMember($_).CustomAttributes.Where( | ||
{ | ||
$_.AttributeType.Name -eq 'DscPropertyAttribute' | ||
} | ||
) | ||
} | ||
|
||
if (-not [System.String]::IsNullOrEmpty($property)) | ||
{ | ||
if ($PSBoundParameters.ContainsKey('Attribute')) | ||
{ | ||
$propertiesOfAttribute = @() | ||
|
||
$propertiesOfAttribute += $property | Where-Object -FilterScript { | ||
$InputObject.GetType().GetMember($_).CustomAttributes.Where( | ||
{ | ||
<# | ||
To simplify the code, ignoring that this will compare | ||
MemberNAme against type 'Optional' which does not exist. | ||
#> | ||
$_.NamedArguments.MemberName -in $Attribute | ||
} | ||
).NamedArguments.TypedValue.Value -eq $true | ||
} | ||
|
||
# Include all optional parameter if it was requested. | ||
if ($Attribute -contains 'Optional') | ||
{ | ||
$propertiesOfAttribute += $property | Where-Object -FilterScript { | ||
$InputObject.GetType().GetMember($_).CustomAttributes.Where( | ||
{ | ||
$_.NamedArguments.MemberName -notin @('Key', 'Mandatory', 'NotConfigurable') | ||
} | ||
) | ||
} | ||
} | ||
|
||
$property = $propertiesOfAttribute | ||
} | ||
} | ||
|
||
# Return a hashtable containing each key property and its value. | ||
$getPropertyResult = @{} | ||
|
||
foreach ($currentProperty in $property) | ||
{ | ||
if ($HasValue.IsPresent) | ||
{ | ||
$isAssigned = Test-DscPropertyIsAssigned -Name $currentProperty -InputObject $InputObject | ||
|
||
if (-not $isAssigned) | ||
{ | ||
continue | ||
} | ||
} | ||
|
||
$getPropertyResult.$currentProperty = $InputObject.$currentProperty | ||
} | ||
|
||
return $getPropertyResult | ||
} | ||
} |
Oops, something went wrong.