我的脚本中有这种情况:
$Excludes = "Commvault","Veeam"
$testuser = 'DOMAIN\vCommvaultConnect'
$Excludes | ForEach-Object {
If ($testuser.Contains($_)) {
Write-Host "Found"
}
}
这是最有效的测试方法,还是有一种更快的方法来将用户与每个排除的单词匹配?
如何,使用正则表达式搜索组
$Excludes = "Commvault","Veeam"
$SearchRegex_Excludes = ($Excludes | % { "(" + ($_) + ")" }) -join "|"
# sample regex pattern result - (Commvault)|(Veeam)
$testuser = "DOMAIN\vCommvaultConnect"
if ( $testuser -match $SearchRegex_Excludes ) { "Found" } else { "Not Found " }