Skip to content

Latest commit

 

History

History
141 lines (119 loc) · 2.61 KB

DSCUseIdenticalMandatoryParametersForDSC.md

File metadata and controls

141 lines (119 loc) · 2.61 KB
description ms.custom ms.date ms.topic title
Use identical mandatory parameters for DSC Get/Test/Set TargetResource functions
PSSA v1.21.0
06/28/2023
reference
DSCUseIdenticalMandatoryParametersForDSC

UseIdenticalMandatoryParametersForDSC

Severity Level: Error

Description

For script based DSC resources, if a property is declared with attributes Key of Required in a mof file, then is should be present as a mandatory parameter in the corresponding Get-TargetResource, Set-TargetResource and Test-TargetResource functions.

How

Make sure all the properties with Key and Required attributes have equivalent mandatory parameters in the Get/Set/Test functions.

Example

Consider the following mof file.

class WaitForAny : OMI_BaseResource
{
    [key, Description("Name of Resource on remote machine")]
    string Name;

    [required, Description("List of remote machines")]
    string NodeName[];
};

Wrong

function Get-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message
    )
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name
    )
}

function Test-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name
    )
}

Correct

function Get-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name
    )
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name
    )
}

function Test-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Message,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Name
    )
}