Powershell 筛选名称列表
Powershell filter a list of names
嘿嘿,对于我正在制作的一个小脚本,我有一个从活动目录中获得的大量用户列表,
例如:
$userlist = Get-ADUser -Filter *
我已经将搜索范围缩小到只搜索我希望它搜索的文件夹..但是那里仍然有一些“尸体”,一些不需要在列表中的死账户,但这也无法从域中删除。如何从列表中过滤掉这些帐户?
以下是每个用户在我的列表中的字段:
screenshot 来自 Get-ADUser 的输出
如果可能的话,我想过滤掉我放入变量的另一个文本文件中的名称列表。 (我想通过“名称”字段过滤它)
我尝试过以下方法:
$userlist = Get-AdUser -Filter 'Name -notin $Filter'
但这似乎不起作用:(
我设法用一个关键字做到了,但不能用一个列表来完成
$userlist = Get-ADUser -Filter 'Name -notlike "*test*"'
感谢您的帮助!
Active Directory Filter 不支持 -notin
运算符。您可以使用以下 LDAP 过滤器技巧从您的查询中排除这些用户:
# $toExclude could be also pulled from a file, however you need to make
# sure there are no trailling or leading spaces on each line,
# you can use `.Trim()` for that.
#
# $toExclude = (Get-Content userstoexclude.txt).ForEach('Trim')
$toExclude = 'user.example1', 'user.example2', 'user.example3'
$filter = '(&(!name={0}))' -f ($toExclude -join ')(!name=')
# LDAP Filter would look like this:
# (&(!name=user.example1)(!name=user.example2)(!name=user.example3))
$userList = Get-ADUser -LDAPFilter $filter
如果您有兴趣了解有关查询的 LDAP 语法的更多信息,您可能需要查看:
嘿嘿,对于我正在制作的一个小脚本,我有一个从活动目录中获得的大量用户列表, 例如:
$userlist = Get-ADUser -Filter *
我已经将搜索范围缩小到只搜索我希望它搜索的文件夹..但是那里仍然有一些“尸体”,一些不需要在列表中的死账户,但这也无法从域中删除。如何从列表中过滤掉这些帐户? 以下是每个用户在我的列表中的字段: screenshot 来自 Get-ADUser 的输出
如果可能的话,我想过滤掉我放入变量的另一个文本文件中的名称列表。 (我想通过“名称”字段过滤它)
我尝试过以下方法:
$userlist = Get-AdUser -Filter 'Name -notin $Filter'
但这似乎不起作用:( 我设法用一个关键字做到了,但不能用一个列表来完成
$userlist = Get-ADUser -Filter 'Name -notlike "*test*"'
感谢您的帮助!
Active Directory Filter 不支持 -notin
运算符。您可以使用以下 LDAP 过滤器技巧从您的查询中排除这些用户:
# $toExclude could be also pulled from a file, however you need to make
# sure there are no trailling or leading spaces on each line,
# you can use `.Trim()` for that.
#
# $toExclude = (Get-Content userstoexclude.txt).ForEach('Trim')
$toExclude = 'user.example1', 'user.example2', 'user.example3'
$filter = '(&(!name={0}))' -f ($toExclude -join ')(!name=')
# LDAP Filter would look like this:
# (&(!name=user.example1)(!name=user.example2)(!name=user.example3))
$userList = Get-ADUser -LDAPFilter $filter
如果您有兴趣了解有关查询的 LDAP 语法的更多信息,您可能需要查看: