Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Severity Level: Information
Default state: Always enabled
Description
This rule detects if functions in Desired State Configuration (DSC) resources have specific return objects. You'll need to ensure that each function returns the correct type.
For non-class based resources:
Get-TargetResourcemust return a hash table.Set-TargetResourcemust not return any value.Test-TargetResourcemust return a boolean.
For class based resources:
Getmust return an instance of the DSC class.Setmust not return any value.Testmust return a boolean.
Example
Noncompliant MOF-based resource
function Get-TargetResource
{
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
function Set-TargetResource
{
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
function Test-TargetResource
{
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
Compliant MOF-based resource
function Get-TargetResource
{
[OutputType([Hashtable])]
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
function Set-TargetResource
{
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
function Test-TargetResource
{
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[String]
$Name
)
...
}
Noncompliant class-based resource
[DscResource()]
class MyDSCResource
{
[DscProperty(Key)]
[string] $Name
[void] Set()
{
...
}
[bool] Test()
{
...
}
}
Compliant class-based resource
[DscResource()]
class MyDSCResource
{
[DscProperty(Key)]
[string] $Name
[MyDSCResource] Get()
{
...
}
[void] Set()
{
...
}
[bool] Test()
{
...
}
}
Configure rule
This rule is always enabled and isn't configurable. Use one of the following methods to avoid using this rule:
- Create a custom rule configuration file to include only the rules you want or exclude the rules you don't want.
- Add the appropriate rule suppression attributes to your code to suppress the rule for specific code blocks. For more information, see the Suppressing rules section of Using PSScriptAnalyzer.