我的脚本中有这种情况:
$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 " }
关于Powershell 包含 - 匹配部分字符串和列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60667890/