powershell - -like,-匹配子数组
问题描述
我正在努力从 Active Directory 中获取非活动用户列表,Search-ADAccount
然后通过管道将其传输到,Get-ADUser
以便我可以使用 whereMemberOf
不包含该组"Accounts_to_Keep"
。我相信我可以使用正确的数字 ( 379
) 和完整的 DN 字符串。但是,如果组移动,我想使用-match
或-like
仅使用组的名称。它返回的数字不一样。
如果我单独对单个用户执行此操作,MemberOf
它只会过滤掉一个组并返回用户拥有的另一个组,所以我认为这就是为什么我拥有的不仅仅是-contains
. 有没有办法在不自己使用子数组的情况下使用-like
或-match
用于子数组foreach
?
从字符串中删除完整的 DN
PS> $InactiveAll.Count
488
PS> ($InactiveAll | Where {-not $_.memberof.contains("CN=Accounts_to_Keep,OU=DC")}).Count
379
PS> ($InactiveAll | Where { $_.memberof -notlike "*Accounts_To_keep*"}).Count
427
PS> ($InactiveAll | Where {-not $_.memberof -contains ("CN=Accounts_to_Keep,OU=DC")}).Count
61
PS> ($InactiveAll | Where {-not ($_.memberof -contains ("CN=Accounts_to_Keep,OU=DC"))}).Count
379
PS> ($InactiveAll | Where { $_.memberof -notmatch "Accounts_To_Keep"}).Count
427
解决方案
-like 和 -notlike 使用通配符,"*"
. 此外,在组数组上使用 -notlike 和 -notmatch 与在单个元素上使用它们的结果不同。我认为您需要研究这些运营商的工作。任何结果都将在 where-object 中评估为“真”。
'group1','group2','group3' -notmatch 'group1'
group2
group3
'group1','group2','group3' -notlike '*group1*'
group2
group3
这是一种在字符串数组中搜索子字符串的方法:
| where { -not ($_.memberof | select-string group1) }
或者
| where { -not ($_.memberof -match 'group1') }
| where { -not ($_.memberof -like '*group1*') }
推荐阅读
- pandas - 按自定义顺序订购情节图例
- c# - C# & ASP.NET MVC:如何在另一个模型中填充数据对象模型
- visual-studio - Visual Studio 2019:调试期间“本地”面板出错 - “检索本地值的内部错误”
- react-native - React-Redux 不更新 SetInterval 上的值
- postgresql - 返回单个值 PSQL - 查询没有结果数据的目的地
- php - 如何在 Wordpress rest_api_init 回调函数中传递参数
- javascript - 如何将图像上传到画布并在其上绘制正方形并获取正方形的坐标
- r - 如何left_join()两个数据集,但仅从其中一个数据集中选择特定列?
- c# - 如何从缓冲区中读取 x 数量
- spring-boot - Spring boot JSF可执行JAR找不到xhtml文件