Test-DscTargetResource

Tests that all the properties on a resource and object are the same.

Syntax

Test-DscTargetResource [-TargetResource] <Hashtable> [-DesiredResource] <Hashtable> [-Target] <String> [<CommonParameters>]

Description

DSC expects a resource's Test-TargetResource function to return $false if an object needs to be updated. Usually, you compare the current state of a resource with the desired state, and return $false if anything doesn't match.

This function takes in a hashtable of the current resource's state (what's returned by Get-TargetResource) and compares it to the desired state (the values passed to Test-TargetResource). If any property in the target resource is different than the desired resource, a list of stale resources is written to the verbose stream and $false is returned.

Here's a quick example:

return Test-TargetResource -TargetResource (Get-TargetResource -Name 'fubar') -DesiredResource $PSBoundParameters -Target ('my resource ''fubar''')

If you want to exclude properties from the evaluation, just remove them from the hashtable returned by Get-TargetResource:

$resource = Get-TargetResource -Name 'fubar'
$resource.Remove( 'PropertyThatDoesNotMatter' )
return Test-TargetResource -TargetResource $resource -DesiredResource $PSBoundParameters -Target ('my resource ''fubar''')

Test-DscTargetResource is new in Carbon 2.0.

Parameters

Name Type Description Required? Pipeline Input Default Value
TargetResource Hashtable

The current state of the resource.

true false
DesiredResource Hashtable

The desired state of the resource. Properties not in this hashtable are skipped. Usually you'll pass PSBoundParameters from your Test-TargetResource function.

true false
Target String

The a description of the target object being tested. Output in verbose messages.

true false

Return Values

System.Boolean.

EXAMPLE 1

Test-TargetResource -TargetResource (Get-TargetResource -Name 'fubar') -DesiredResource $PSBoundParameters -Target ('my resource ''fubar''')

Demonstrates how to test that all the properties on a DSC resource are the same was what's desired.