Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell包含-部分字符串和列表是否匹配?_Powershell_Match_Contains - Fatal编程技术网

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”我确实测量过。好啊如果这种差异对你来说很重要你能解释一下为什么这更有效吗?