Skip to content

Counts

Each says that every element of an array meets a condition. Count says how many do, and is tested with Must like any number: -Be, -BeIn and the four comparisons.

Require 'At least one IP rule' {
    Count networkAcls.ipRules[*] | Must -BeGreaterThan 0
}

Require 'Few rules allow inbound traffic' {
    Count securityRules[*] -Where {
        Field direction | Must -Be 'Inbound'
        Field access | Must -Be 'Allow'
    } | Must -BeLessOrEqual 5
}
  • -Where selects what to count. It is emitted as written, like When: it isn't inverted and gets no missing-field checks. Field names are relative to the element, Element is the element itself, and AnyOf, AllOf and nested counts work inside. Without -Where, every element is counted.
  • A value count counts the members of a list: a literal array, Param or Arm. Current is the member being counted, and -Name gives it a name, which nested counts need:
Require 'The name matches an allowed pattern' {
    Count -Value (Param NamePatterns) -Name pattern -Where {
        Field name | Must -BeLike (Current pattern)
    } | Must -BeGreaterThan 0
}

Current can't name a tag: Tag (Current tagName) is a compile error (BYL1059), because Azure rejects a field named with current(). To require every tag in a list, test the tags object for each name instead:

Require 'Every mandatory tag is set' {
    Count -Value (Param MandatoryTags) -Name tagName -Where {
        Field tags | Must -ContainKey (Current tagName)
    } | Must -Be (Param MandatoryTags -Length)
}

To test the tags' values, check each tag with its own Tag.

  • To compare with the length of a list, use Param Name -Length. Here every pattern must match, not just one:
Require 'The name matches every pattern' {
    Count -Value (Param NamePatterns) -Name pattern -Where {
        Field name | Must -BeLike (Current pattern)
    } | Must -Be (Param NamePatterns -Length)
}
  • In Require, the comparison is inverted like any other: -BeGreaterThan 0 emits lessOrEquals 0. A count is never missing, so it gets no guard.
  • Each path[*] { P } is the same as Count path[*] -Where { <violation of P> } | Must -Be 0, and stays the simple way to say "every element".
  • Azure allows at most 5 field counts per array, 10 value counts per rule, and 100 iterations over literal lists. The build checks all three.