Powershell 包含 - 匹配部分字符串和列表?
Powershell contains - match on partial string and list?
我的脚本中有这种情况:
$Excludes = "Commvault","Veeam"
$testuser = 'DOMAIN\vCommvaultConnect'
$Excludes | ForEach-Object {
If ($testuser.Contains($_)) {
Write-Host "Found"
}
}
这是对此进行测试的最有效方法,还是有更快的方法将用户与每个排除的词相匹配?
怎么样,使用 Regex 搜索组
$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 " }
我的脚本中有这种情况:
$Excludes = "Commvault","Veeam"
$testuser = 'DOMAIN\vCommvaultConnect'
$Excludes | ForEach-Object {
If ($testuser.Contains($_)) {
Write-Host "Found"
}
}
这是对此进行测试的最有效方法,还是有更快的方法将用户与每个排除的词相匹配?
怎么样,使用 Regex 搜索组
$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 " }