Powershell包含-部分字符串和列表是否匹配?
我的剧本中有这种情况:Powershell包含-部分字符串和列表是否匹配?,powershell,match,contains,Powershell,Match,Contains,我的剧本中有这种情况: $Excludes = "Commvault","Veeam" $testuser = 'DOMAIN\vCommvaultConnect' $Excludes | ForEach-Object { If ($testuser.Contains($_)) { Write-Host "Found" } } 这是最有效的测试方法,还是有更快的方法将用户与每个被排除的单词进行匹配?使用Regex搜索组如何 $Excludes = "Commvault
$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 " }
这比原来的更有效还是更快?如果是-为什么?两者都是,只需在两者上使用measure命令即可。谢谢你,这就是我想记住的方法。excludes变量仅用于此目的,因此我可以将其定义为正则表达式组:$excludes=“Commvault | Veeam”我确实测量过。好啊如果这种差异对你来说很重要你能解释一下为什么这更有效吗?