Skip to content

Commit 0ea0938

Browse files
author
Kapil Borle
committed
Fix UseIdenticalMandatoryParametersForDSC documentation
1 parent 97c1473 commit 0ea0938

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

RuleDocumentation/UseIdenticalMandatoryParametersForDSC.md

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
## Description
66

7-
For script based DSC resources the `Get-TargetResource`, `Test-TargetResource` and `Set-TargetResource` functions must have identical mandatory parameters that are also the keys in the corresponding `mof` file.
7+
For script based DSC resources the `Get-TargetResource`, `Test-TargetResource` and `Set-TargetResource` functions:
8+
9+
1. If a parameter is declared as `mandatory` in any of the `Get/Set/Test` functions, then it should be a mandatory parameter in all the three functions.
10+
1. 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 `Get/Set/Test` functions of the corresponding resource file.
811

912
## How
1013

11-
Make sure all the keys have equivalent mandatory parameters in the `Get/Set/Test` functions.
14+
1. Make sure `Get/Set/Test` declare identical mandatory parameters.
15+
1. Make sure all the properties with `Key` and `Required` attributes have equivalent mandatory parameters in the `Get/Set/Test` functions.
1216

1317
## Example
18+
1419
Consider the following `mof` file.
20+
1521
```powershell
1622
class WaitForAny : OMI_BaseResource
1723
{
@@ -28,37 +34,48 @@ class WaitForAny : OMI_BaseResource
2834
``` PowerShell
2935
function Get-TargetResource
3036
{
31-
[OutputType([Hashtable])]
37+
[CmdletBinding()]
3238
param
3339
(
34-
[parameter(Mandatory = $true)]
40+
[Parameter(Mandatory = $true)]
41+
[ValidateNotNullOrEmpty()]
3542
[String]
36-
$Name
43+
$Message
3744
)
38-
...
3945
}
4046
4147
function Set-TargetResource
4248
{
49+
[CmdletBinding()]
4350
param
4451
(
45-
[parameter(Mandatory = $true)]
52+
[Parameter(Mandatory = $true)]
53+
[ValidateNotNullOrEmpty()]
4654
[String]
47-
$TargetName # this should be Name
55+
$Message,
56+
57+
[Parameter(Mandatory = $true)]
58+
[ValidateNotNullOrEmpty()]
59+
[String]
60+
$Name
4861
)
49-
...
5062
}
5163
5264
function Test-TargetResource
5365
{
54-
[OutputType([System.Boolean])]
66+
[CmdletBinding()]
5567
param
5668
(
57-
[parameter(Mandatory = $true)]
69+
[Parameter(Mandatory = $true)]
70+
[ValidateNotNullOrEmpty()]
71+
[String]
72+
$Message,
73+
74+
[Parameter(Mandatory = $true)]
75+
[ValidateNotNullOrEmpty()]
5876
[String]
5977
$Name
6078
)
61-
...
6279
}
6380
```
6481

@@ -67,36 +84,52 @@ function Test-TargetResource
6784
``` PowerShell
6885
function Get-TargetResource
6986
{
70-
[OutputType([Hashtable])]
87+
[CmdletBinding()]
7188
param
7289
(
73-
[parameter(Mandatory = $true)]
90+
[Parameter(Mandatory = $true)]
91+
[ValidateNotNullOrEmpty()]
92+
[String]
93+
$Message,
94+
95+
[Parameter(Mandatory = $true)]
96+
[ValidateNotNullOrEmpty()]
7497
[String]
7598
$Name
7699
)
77-
...
78100
}
79101
80102
function Set-TargetResource
81103
{
104+
[CmdletBinding()]
82105
param
83106
(
84-
[parameter(Mandatory = $true)]
107+
[Parameter(Mandatory = $true)]
108+
[ValidateNotNullOrEmpty()]
109+
[String]
110+
$Message,
111+
112+
[Parameter(Mandatory = $true)]
113+
[ValidateNotNullOrEmpty()]
85114
[String]
86115
$Name
87116
)
88-
...
89117
}
90118
91119
function Test-TargetResource
92120
{
93-
[OutputType([System.Boolean])]
121+
[CmdletBinding()]
94122
param
95123
(
96-
[parameter(Mandatory = $true)]
124+
[Parameter(Mandatory = $true)]
125+
[ValidateNotNullOrEmpty()]
126+
[String]
127+
$Message,
128+
129+
[Parameter(Mandatory = $true)]
130+
[ValidateNotNullOrEmpty()]
97131
[String]
98132
$Name
99133
)
100-
...
101134
}
102135
```

0 commit comments

Comments
 (0)