Closed LaurentDardenne closed 5 years ago
@LaurentDardenne Just confirming that I've understood you correctly. You want to create an output message that is dynamic such as based on target object.
Exact. String substitution from $TargetObject may be sufficient. And if possible allow message localization.
After reflection, I miss in the result emitted by PSRule a property of type Object / PSObject and not only a string. The ToString() method of such an object could create a summary of the cause of the error from its various properties that would be populated in the body of the rule. Please tell me if I divert the use of PSRule (rule engine) which should only issue $true / $false in result.
I tried to use Pester as a rule engine but I had the same problem, the result of pester only concerns the success or failure of a test. What is his goal ;-)
@LaurentDardenne PSRule will error if a non-boolean result is returned.
I have a plan for this, to be able to return condition messages.
But you could use Write-Information
(which is supported) but it's not really the same.
@LaurentDardenne Working through the implementation for this now.
Planning on:
Exists
, Match
, Within
and TypeOf
will automatically provide a localized reason. Or this can be set to a custom reason with the -Reason
parameter.Reason
keyword will be introduced to add a reason for custom logic. Similar to the Report
keyword suggested in your example.Reason
will be able to be called multiple times.Reason
property which will be an array of reasons, however Reason
will only be set if the rule returned an outcome of Fail
or Error
. Reason
will not be set for Pass
or None
outcomes.Reason
keyword can be localized by using the $LocalizedData
variable introduced in v0.7.0.Thank you very much for these additions. I'm testing it soon.
Maybe add in the documentation (about_PSRule_Keywords.md) that in the body of a rule, all occurrences of the Reason property are concatenated. For example :
@'
Rule -Name 'Test' {
Recommend "Use a valid group."
Reason "The group list contains errors."
Write-Warning "$($TargetObject|Out-String)"
Within Group -not 'Group2','Group5' -reason "The group '$($TargetObject.Group)' is not allowed."
Match 'Group' '^Group(1|2|3)$' -reason "Naming convention error."
}
'@ > "$Path\$File"
$result|? {$_.outcome -eq 'fail'}|select TargetObject,Reason
TargetObject Reason
------------ ------
@{Name=Name2; Group=Group2} {The group list contains errors., The group 'Group2' is not allowed.}
@{Name=Name4; Group=Group4} {The group list contains errors., Naming convention error.}
@{Name=Name5; Group=Group5} {The group list contains errors., The group 'Group5' is not allowed., Naming convention error.}
This makes it possible to check several rules on the same object and to obtain a single report. Bravo !
@LaurentDardenne Yes I encountered this a little when I added the -OutputFormat Wide
view. I added a GetReasonViewString()
method to create a concatenated string. But your question makes me think there may be a better way.
There is a missing property on the RuleRecord object that would allow you to create a scan report for users. If I analyze a data file created by a user it can contain input errors, rules will therefore check the error cases. The user may not know the rules or have to interpret the result. I can of course create associations between a name of rules + a state AND an error message, but a dedicated field in the body of the rule would be simple I think.
Note that the misuse of the 'Recommendation' property always refers to the first object received in the pipeline.
Describe alternatives you've considered