ReturnCorrectTypesForDSCFunctions
Severity Level: Information
Description
The functions in DSC resources have specific return objects.
For non-class based resources:
Set-TargetResource
must not return any value.Test-TargetResource
must return a boolean.Get-TargetResource
must return a hash table.
For class based resources:
Set
must not return any value.Test
must return a boolean.Get
must return an instance of the DSC class.
How
Ensure that each function returns the correct type.
Example 1
Wrong
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
)
...
}
Correct
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
)
...
}
Example 2
Wrong
[DscResource()]
class MyDSCResource
{
[DscProperty(Key)]
[string] $Name
[String] Get()
{
...
}
[String] Set()
{
...
}
[bool] Test()
{
...
}
}
Correct
[DscResource()]
class MyDSCResource
{
[DscProperty(Key)]
[string] $Name
[MyDSCResource] Get()
{
...
}
[void] Set()
{
...
}
[bool] Test()
{
...
}
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for