Heyho,對于我正在制作的一個小腳本,我有一個從活動目錄中獲取的大量用戶串列,例如:
$userlist = Get-ADUser -Filter *
我已經將其范圍縮小到僅搜索我希望它搜索的檔案夾.. 但是那里仍然有一些“尸體”,一些不需要在串列中的死帳戶,但這也可以'不會從域中洗掉。如何將這些帳戶從串列中過濾掉?以下是每個用戶在我的串列中的欄位: Get-ADUser 輸出的螢屏截圖
如果可能的話,我想過濾掉我放入變數的另一個文本檔案中的名稱串列。(我想按“名稱”欄位過濾它)
我已經嘗試過以下方式:
$userlist = Get-AdUser -Filter 'Name -notin $Filter'
但這似乎不起作用:(我設法用一個關鍵字來做到這一點,但不能用一個串列來完成
$userlist = Get-ADUser -Filter 'Name -notlike "*test*"'
謝謝您的幫助!
uj5u.com熱心網友回復:
Active Directory 篩選器不支持-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 語法的更多資訊,您可能需要查看:
- Active Directory:LDAP 語法過濾器
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418469.html
標籤:
