[英]Fastest way to count ad users in PowerShell?
我一直在尋找最快的方法來計算Powershell中的廣告用戶數量以及啟用和禁用的用戶數量,但是查詢非常長(〜100k用戶,每個查詢1-2分鍾),而我的Powershell ISE通常在一個或兩個請求后崩潰(用於報告作業)
所以我的問題是如何優化這些查詢:
$CountADUsers = (get-aduser –filter * -server $myserver).count.Count
$CountADUsersEnabled = (get-aduser -filter * -server $myserver | where {$_.enabled -eq "True"}).count
$CountADUsersNotEnabled = $CountADUsers - $CountADUsersEnabled
在此先感謝大家
您無需兩次運行Get-AdUser。 您可以將其保存為變量,然后對其進行過濾:
$allUsers = get-aduser –filter * -server $myserver
$CountADUsers = $allUsers.count
$CountADUsersEnabled = ($allUsers | where {$_.enabled -eq "True"}).count
同樣,在這種情況下它不會有幫助,但是請記住,使用-Filter *
然后使用Where-Object
並不是很有效,因為您可以使用:
Get-ADUser -Filter {enabled -eq "True"}
另一個提示:不應將ISE用於運行腳本,因為它有時會以奇怪的方式運行(尤其是當計算機內存不足時)。 您應該改用powershell.exe
。
編輯:要改善更多,您可以嘗試僅選擇需要使用的屬性
$t = $allusers |select userprincipalname,enabled
然后使用Where-Object
進行過濾。 為了比較:
Measure-command {($allusers | where {$_.enabled -eq "True"}).count}
花了兩分鍾
Measure-command {($t | where {$_.enabled -eq "True"}).count}
花費了兩秒鍾的時間(但是選擇大約需要2分鍾,因此總體時間大致相同)。 但是,這在很大程度上取決於方案。 我將它留給您,以便您找到適合您情況的最佳解決方案。 記住, Measure-Command
是您的好朋友!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.