gpt4 book ai didi

Powershell 包含 - 匹配部分字符串和列表?

转载 作者:行者123 更新时间:2023-12-02 02:39:37 27 4
gpt4 key购买 nike

我的脚本中有这种情况:

$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 " }

关于Powershell 包含 - 匹配部分字符串和列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667890/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com