-
Notifications
You must be signed in to change notification settings - Fork 225
/
ConvertTo-Reason.ps1
79 lines (69 loc) · 2.67 KB
/
ConvertTo-Reason.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<#
.SYNOPSIS
Returns a array of the type `[Reason]`.
.DESCRIPTION
This command converts the array of properties that is returned by the command
`Compare-DscParameterState`. The result is an array of the type `[Reason]` that
can be returned in a DSC resource's property **Reasons**.
.PARAMETER Property
The result from the command Compare-DscParameterState.
.EXAMPLE
ConvertTo-Reason -Property (Compare-DscParameterState)
.OUTPUTS
[Reason[]]
#>
function ConvertTo-Reason
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when the output type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
[CmdletBinding()]
[OutputType([Reason[]])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AllowEmptyCollection()]
[AllowNull()]
[System.Collections.Hashtable[]]
$Property,
[Parameter(Mandatory = $true)]
[System.String]
$ResourceName
)
begin
{
# Always return an empty array if there are no properties to add.
$reasons = [Reason[]] @()
}
process
{
foreach ($currentProperty in $Property)
{
if ($currentProperty.ExpectedValue -is [System.Enum])
{
# Return the string representation of the value (instead of the numeric value).
$propertyExpectedValue = $currentProperty.ExpectedValue.ToString()
}
else
{
$propertyExpectedValue = $currentProperty.ExpectedValue
}
if ($property.ActualValue -is [System.Enum])
{
# Return the string representation of the value so that conversion to json is correct.
$propertyActualValue = $currentProperty.ActualValue.ToString()
}
else
{
$propertyActualValue = $currentProperty.ActualValue
}
$reasons += [Reason] @{
Code = '{0}:{0}:{1}' -f $ResourceName, $currentProperty.Property
# Convert the object to JSON to handle complex types.
Phrase = 'The property {0} should be {1}, but was {2}' -f $currentProperty.Property, ($propertyExpectedValue | ConvertTo-Json -Compress), ($propertyActualValue | ConvertTo-Json -Compress)
}
}
}
end
{
return $reasons
}
}