Failing a rule deliberately with custom statement #1143
-
I have 2 questions:
Rule 'ManagedIdentity3' -If { $TargetObject.RoleDefinitionName -eq $configuration.ServiceBusPermission }{
$expectedQueues = $configuration.ServiceBusQueues.Split(',')
$actualValue = $TargetObject.RoleAssignmentId
$bFlag = $false
Foreach($item in $expectedQueues){
if($actualValue.Contains($item)){
$bFlag = $true
$Assert.Contains($TargetObject,'RoleAssignmentId',$item)
}
}
if($bFlag -eq $false){
**#Want to fail the rule with a message**
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@ankur90Git Thanks for the questions.
A reason is more dynamic, it's intended to report the specific thing that failed. An example might be: "A Contributor RBAC assignment was not found." You can use both together. Reasons are only shown if the rule failed. They are not shown for failure by default when using Recommendations are more like metadata for the rule so they are always available on the result object. They are shown by default when using In terms of your specific case I suggest using a reason. For example you could add the following: Rule 'ManagedIdentity3' -If { $TargetObject.RoleDefinitionName -eq $configuration.ServiceBusPermission }{
$expectedQueues = $configuration.ServiceBusQueues.Split(',')
$actualValue = $TargetObject.RoleAssignmentId
$bFlag = $false
Foreach($item in $expectedQueues){
if($actualValue.Contains($item)){
$bFlag = $true
$Assert.Contains($TargetObject,'RoleAssignmentId',$item)
}
}
if($bFlag -eq $false){
$Assert.Fail("Some reason why this failed.")
}
} Extending on this you could also write as: Rule 'ManagedIdentity3' -If { $TargetObject.RoleDefinitionName -eq $configuration.ServiceBusPermission }{
$expectedQueues = $configuration.ServiceBusQueues.Split(',')
$actualValue = $TargetObject.RoleAssignmentId
$bFlag = $false
Foreach($item in $expectedQueues){
if($actualValue.Contains($item)){
$bFlag = $true
$Assert.Contains($TargetObject,'RoleAssignmentId',$item)
}
}
$Assert.Create($bFlag, "Some reason why this failed.")
} Or use arguments in the reason like: Rule 'ManagedIdentity3' -If { $TargetObject.RoleDefinitionName -eq $configuration.ServiceBusPermission }{
$expectedQueues = $configuration.ServiceBusQueues.Split(',')
$actualValue = $TargetObject.RoleAssignmentId
$bFlag = $false
Foreach($item in $expectedQueues){
if($actualValue.Contains($item)){
$bFlag = $true
$Assert.Contains($TargetObject,'RoleAssignmentId',$item)
}
}
$Assert.Create($bFlag, "Some reason why this failed for role assignment: {0}.", $TargetObject.RoleAssignmentId)
} Some more options are here: Authoring assertion methods.
A synopsis and recommendations can be added easily by setting the To add a synopsis/ recommendation try: # Synopsis: Some recommandations/ synopsis in a single line. Multi-lines are not supported via this comment.
Rule 'ManagedIdentity3' -If { $TargetObject.RoleDefinitionName -eq $configuration.ServiceBusPermission }{
$expectedQueues = $configuration.ServiceBusQueues.Split(',')
$actualValue = $TargetObject.RoleAssignmentId
$bFlag = $false
Foreach($item in $expectedQueues){
if($actualValue.Contains($item)){
$bFlag = $true
$Assert.Contains($TargetObject,'RoleAssignmentId',$item)
}
}
$Assert.Create($bFlag, "Some reason why this failed for role assignment: {0}.", $TargetObject.RoleAssignmentId)
} Alternatively, you could use I hope that helps. |
Beta Was this translation helpful? Give feedback.
@ankur90Git Thanks for the questions.
A reason is more dynamic, it's intended to report the specific thing that failed. An example might be: "A Contributor RBAC assignment was not found."
You can use both together.
Reasons are only shown if the rule failed. They are not shown for failure by default when using
Invoke-PSRule
but can be shown then using-OutputFormat Wide
. They are always shown on failures when usingAssert-PS…