Pester/PowerShell 输出应该 match/contain $a 或 $b
Pester/PowerShell output should match/contain either $a or $b
我想请教检查 public IP 是否附加到 $edge00
或 $edge01
NIC
# id of the Get-AzPublicIpAddress output will = "/subscriptions/asdf-asdf-asdf/resourceGroups/RG-ASDF-FW/providers/Microsoft.Network/networkInterfaces/FW-ASDF-EDGE-00-NIC1/ipConfigurations/FW-ASDF-EDGE-ASDF"
$edge00 = "FW-ASDF-EDGE-00-NIC1"
$edge01 = "FW-ASDF-EDGE-01-NIC1"
# this will fail
(Get-AzPublicIpAddress -Name "PIP-FW-ASDF-EDGE-UNTRUST" -ResourceGroupName "RG-ASDF-FW").IpConfiguration.Id | Should -Match ($edge00 -or $edge01)
# this will work
(Get-AzPublicIpAddress -Name "PIP-FW-ASDF-EDGE-UNTRUST" -ResourceGroupName "RG-ASDF-FW").IpConfiguration.Id | Should -Match $edge00
我已经做了很多搜索,但我无法通过普通的 PowerShell 命令或 Pester 找到一种方法来检查字符串 ($id
) 是否包含 string1 ($edge00
)或 string2 ($edge01
)
请问大家有什么想法吗?
因为使用交替 (|
) 的 boxdog suggests, using Should
's -Match
parameter with a regular expression 匹配多个 子串中的任何一个 ,就像 PowerShell 的 -match
运算符:
$edge00 = 'bar'
$edge01 = 'foo'
# Each input must contain substring 'bar' or substring 'foo'
'food', 'rebar' | Should -Match "$edge00|$edge01' # OK
需要注意的是,如果您的子表达式来自变量,就像您的情况一样,通常最好使用 [regex]::Escape()
来确保它们被视为 文字 :
"$([regex]::Escape($edge00))|$([regex]::Escape($edge01))"
.
这里不是绝对必要的,但是对于事先不知道具体变量内容的场景,还是要注意的。
此外,您可能希望匹配受到更多限制以排除误报:
"/$([regex]::Escape($edge00))/|/$([regex]::Escape($edge01))/"
.
如果你想匹配整个个字符串,字面上,使用-BeIn
,其工作方式类似于 PowerShell 的 -in
运算符:
# Each input must be either 'rebar' or 'food'
'food', 'rebar' | Should -BeIn rebar, food # OK
在您的情况下,您可以预处理您的输入以提取感兴趣的标记,然后允许您使用-BeIn
,而不用担心转义或误报;一个简化的例子:
$edge00 = 'FW-ASDF-EDGE-00-NIC1'
$edge01 = 'FW-ASDF-EDGE-01-NIC1'
# Extract the 3rd-from-last token from each path, which must either
# be $edge00 or $edge01
('/.../.../networkInterfaces/FW-ASDF-EDGE-00-NIC1/more/stuff',
'/.../.../networkInterfaces/FW-ASDF-EDGE-01-NIC1/more/stuff' -split '/')[-3] |
Should -BeIn $edge00, $edge01 # OK
这种方法还避免了对单独变量的需要,因为您可以改为定义单个数组变量 - $edges = 'FW-ASDF-EDGE-00-NIC1', 'FW-ASDF-EDGE-01-NIC1'
- 并将其传递给 -BeIn
.
我想请教检查 public IP 是否附加到 $edge00
或 $edge01
NIC
# id of the Get-AzPublicIpAddress output will = "/subscriptions/asdf-asdf-asdf/resourceGroups/RG-ASDF-FW/providers/Microsoft.Network/networkInterfaces/FW-ASDF-EDGE-00-NIC1/ipConfigurations/FW-ASDF-EDGE-ASDF"
$edge00 = "FW-ASDF-EDGE-00-NIC1"
$edge01 = "FW-ASDF-EDGE-01-NIC1"
# this will fail
(Get-AzPublicIpAddress -Name "PIP-FW-ASDF-EDGE-UNTRUST" -ResourceGroupName "RG-ASDF-FW").IpConfiguration.Id | Should -Match ($edge00 -or $edge01)
# this will work
(Get-AzPublicIpAddress -Name "PIP-FW-ASDF-EDGE-UNTRUST" -ResourceGroupName "RG-ASDF-FW").IpConfiguration.Id | Should -Match $edge00
我已经做了很多搜索,但我无法通过普通的 PowerShell 命令或 Pester 找到一种方法来检查字符串 ($id
) 是否包含 string1 ($edge00
)或 string2 ($edge01
)
请问大家有什么想法吗?
因为使用交替 (|
) 的 boxdog suggests, using Should
's -Match
parameter with a regular expression 匹配多个 子串中的任何一个 ,就像 PowerShell 的 -match
运算符:
$edge00 = 'bar'
$edge01 = 'foo'
# Each input must contain substring 'bar' or substring 'foo'
'food', 'rebar' | Should -Match "$edge00|$edge01' # OK
需要注意的是,如果您的子表达式来自变量,就像您的情况一样,通常最好使用 [regex]::Escape()
来确保它们被视为 文字 :"$([regex]::Escape($edge00))|$([regex]::Escape($edge01))"
.
这里不是绝对必要的,但是对于事先不知道具体变量内容的场景,还是要注意的。
此外,您可能希望匹配受到更多限制以排除误报:
"/$([regex]::Escape($edge00))/|/$([regex]::Escape($edge01))/"
.
如果你想匹配整个个字符串,字面上,使用-BeIn
,其工作方式类似于 PowerShell 的 -in
运算符:
# Each input must be either 'rebar' or 'food'
'food', 'rebar' | Should -BeIn rebar, food # OK
在您的情况下,您可以预处理您的输入以提取感兴趣的标记,然后允许您使用-BeIn
,而不用担心转义或误报;一个简化的例子:
$edge00 = 'FW-ASDF-EDGE-00-NIC1'
$edge01 = 'FW-ASDF-EDGE-01-NIC1'
# Extract the 3rd-from-last token from each path, which must either
# be $edge00 or $edge01
('/.../.../networkInterfaces/FW-ASDF-EDGE-00-NIC1/more/stuff',
'/.../.../networkInterfaces/FW-ASDF-EDGE-01-NIC1/more/stuff' -split '/')[-3] |
Should -BeIn $edge00, $edge01 # OK
这种方法还避免了对单独变量的需要,因为您可以改为定义单个数组变量 - $edges = 'FW-ASDF-EDGE-00-NIC1', 'FW-ASDF-EDGE-01-NIC1'
- 并将其传递给 -BeIn
.