powershell 查询 - 我可以使用多少个 OR?

powershell query - how many OR can i use?

我找不到任何关于 powershell 中的过滤器限制的信息 我想使用

查询交换服务器上的许多组
Get-DistributionGroup

但是这些组的数量会很大,我会将此查询分成许多查询(如果这是在过滤器中使用 -OR 参数的限制,则每 50 个)

最后的命令将是这样的:

Get-DistributionGroup -Filter "(Name like xyz) -or (Name like 'abc') or (Name like 'def')"

有人知道在过滤器上使用 -or 有什么限制吗?

或者有更好的方法?

选项 1) 以编程方式构建过滤器

documentation没有提到限制。不过,自动创建过滤器并从文件或变量中检索搜索词会更容易:

# get the search values from a file or variable
$searchTerms = Get-Content "searchTerms.txt"
# OR define the values in your script
$searchTerms = "xyz", "abc", "def"

# build the filter
$filter = ($searchTerms | foreach {"(Name like '$_')"}) -join " -or "
Get-DistributionGroup -Filter $filter

选项 2) 分别按每个搜索词过滤

也可以拆分过滤器,但您必须删除重复项,如下所示:

$searchTerms | foreach {Get-DistributionGroup -Filter "(Name like '$_')"} | select -Unique

选项 3) 回退:查询所有内容并进行自己的过滤

或者,您可以获取所有通讯组并进行自己的过滤:

Get-DistributionGroup | where {
    $name = $_.Name
    return $null -ne $searchTerms.where({$name -like $_}, "First")
}

如果有人感兴趣,可以限制 250 个子过滤器。当发送到交换服务器的命令不成功时,我已经设置了登录服务器, 并获得信息:

   Call Site: SomeProject.PowerShell.Service.PowershellCommand`1.Execute
   Exception Type: 
   Exception Message: 
   Stack Trace: 
   Additional Info: Could not invoke command Get-Group, Details:
 The total number of explicit and implicit subfilters exceeds maximum allowed number of 250.  Processing stopped.

“详情:”后的内容为来自远程交换服务器的消息