-
Notifications
You must be signed in to change notification settings - Fork 9
Compare ResourcePropertyState
Compare current and desired property values for any DSC resource and return a hashtable with the metadata from the comparison.
Compare-ResourcePropertyState [-CurrentValues] <Hashtable> [-DesiredValues] <Hashtable>
[[-Properties] <String[]>] [[-IgnoreProperties] <String[]>] [[-CimInstanceKeyProperties] <Hashtable>]
[<CommonParameters>]
This function is used to compare current and desired property values for any DSC resource, and return a hashtable with the metadata from the comparison.
This introduces another design pattern that is used to evaluate current and
desired state in a DSC resource.
This command is meant to be used in a DSC
resource from both Test and Set.
The evaluation is made in Set
to make sure to only change the properties that are not in the desired state.
Properties that are in the desired state should not be changed again.
This
design pattern also handles when the command Invoke-DscResource
is called
with the method Set
, which with this design pattern will evaluate the
properties correctly.
Note
This design pattern is not widely used in the DSC resource modules in the
DSC Community, the only known use is in SqlServerDsc.
This design pattern
can be viewed as deprecated, and should be replaced with the design pattern
that uses the command Compare-DscParameterState
.
See the other design patterns that uses the command Compare-DscParameterState
or Test-DscParameterState
.
$compareTargetResourceStateParameters = @{
CurrentValues = (Get-TargetResource $PSBoundParameters)
DesiredValues = $PSBoundParameters
}
$propertyState = Compare-ResourcePropertyState @compareTargetResourceStateParameters
$propertiesNotInDesiredState = $propertyState.Where({ -not $_.InDesiredState })
This example calls Compare-ResourcePropertyState with the current state
and the desired state and returns a hashtable array of all the properties
that was evaluated based on the properties pass in the parameter DesiredValues.
Finally it sets a parameter $propertiesNotInDesiredState
that contain
an array with all properties not in desired state.
$compareTargetResourceStateParameters = @{
CurrentValues = (Get-TargetResource $PSBoundParameters)
DesiredValues = $PSBoundParameters
Properties = @(
'Property1'
)
}
$propertyState = Compare-ResourcePropertyState @compareTargetResourceStateParameters
$false -in $propertyState.InDesiredState
This example calls Compare-ResourcePropertyState with the current state
and the desired state and returns a hashtable array with just the property
Property1
as that was the only property that was to be evaluated.
Finally it checks if $false
is present in the array property InDesiredState
.
$compareTargetResourceStateParameters = @{
CurrentValues = (Get-TargetResource $PSBoundParameters)
DesiredValues = $PSBoundParameters
IgnoreProperties = @(
'Property1'
)
}
$propertyState = Compare-ResourcePropertyState @compareTargetResourceStateParameters
This example calls Compare-ResourcePropertyState with the current state
and the desired state and returns a hashtable array of all the properties
except the property Property1
.
$compareTargetResourceStateParameters = @{
CurrentValues = (Get-TargetResource $PSBoundParameters)
DesiredValues = $PSBoundParameters
CimInstanceKeyProperties = @{
ResourceProperty1 = @(
'CimProperty1'
)
}
}
$propertyState = Compare-ResourcePropertyState @compareTargetResourceStateParameters
This example calls Compare-ResourcePropertyState with the current state
and the desired state and have a property ResourceProperty1
who's value
is an array of embedded CIM instances.
The key property for the CIM instances
are CimProperty1
.
The CIM instance key property CimProperty1
is used
to get the unique CIM instance object to compare against from both the current
state and the desired state.
A hashtable containing a key for each property that contain a collection of CimInstances and the value is an array of strings of the CimInstance key properties. @{ Permission = @('State') }
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: False
Position: 5
Default value: @{}
Accept pipeline input: False
Accept wildcard characters: False
The current values that should be compared to to desired values. Normally the values returned from Get-TargetResource.
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The values set in the configuration and is provided in the call to the functions *-TargetResource, and that will be compared against current values. Normally set to $PSBoundParameters.
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
An array of property names, from the keys provided in DesiredValues, that will be ignored in the comparison. If this parameter is left out, all the keys in the DesiredValues will be compared.
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
An array of property names, from the keys provided in DesiredValues, that will be compared. If this parameter is left out, all the keys in the DesiredValues will be compared.
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Returns an array containing a hashtable with metadata for each property that was evaluated.
Metadata Name | Type | Description |
---|---|---|
ParameterName | [System.String] |
The name of the property that was evaluated |
Expected | The type of the property | The desired value for the property |
Actual | The type of the property | The actual current value for the property |
InDesiredState | [System.Boolean] |
Returns $true if the expected and actual value was equal. |
- Assert-BoundParameter
- Assert-ElevatedUser
- Assert-IPAddress
- Assert-Module
- Compare-DscParameterState
- Compare-ResourcePropertyState
- ConvertFrom-DscResourceInstance
- ConvertTo-CimInstance
- ConvertTo-HashTable
- Find-Certificate
- Get-ComputerName
- Get-DscProperty
- Get-EnvironmentVariable
- Get-LocalizedData
- Get-LocalizedDataForInvariantCulture
- Get-PSModulePath
- Get-TemporaryFolder
- Get-UserName
- New-ArgumentException
- New-ErrorRecord
- New-Exception
- New-InvalidDataException
- New-InvalidOperationException
- New-InvalidResultException
- New-NotImplementedException
- New-ObjectNotFoundException
- Remove-CommonParameter
- Set-DscMachineRebootRequired
- Set-PSModulePath
- Test-AccountRequirePassword
- Test-DscParameterState
- Test-DscProperty
- Test-IsNanoServer
- Test-IsNumericType
- Test-ModuleExist