简体   繁体   English

在PowerShell中计算广告用户最快的方法?

[英]Fastest way to count ad users in PowerShell?

I've been looking for the fastest way to count the number of ad users in powershell, aswell as the number of enabled and disabled users, but the queries are very long (~100k users, 1-2 min per query) and my powershell ISE usually crashes after one or two requests (it's for a reporting job) 我一直在寻找最快的方法来计算Powershell中的广告用户数量以及启用和禁用的用户数量,但是查询非常长(〜100k用户,每个查询1-2分钟),而我的Powershell ISE通常在一个或两个请求后崩溃(用于报告作业)

So my question is how can I optimize these queries : 所以我的问题是如何优化这些查询:

$CountADUsers = (get-aduser –filter * -server $myserver).count.Count
$CountADUsersEnabled = (get-aduser -filter * -server $myserver | where {$_.enabled -eq "True"}).count
$CountADUsersNotEnabled = $CountADUsers - $CountADUsersEnabled

Thanks in advance guys 在此先感谢大家

You don't need to run Get-AdUser twice. 您无需两次运行Get-AdUser。 You can save it to variable and just filter it: 您可以将其保存为变量,然后对其进行过滤:

$allUsers = get-aduser –filter * -server $myserver
$CountADUsers = $allUsers.count
$CountADUsersEnabled = ($allUsers | where {$_.enabled -eq "True"}).count

Also, it won't be helpful in that case, but please remember that using -Filter * and then Where-Object is not very effective as you can just use: 同样,在这种情况下它不会有帮助,但是请记住,使用-Filter *然后使用Where-Object并不是很有效,因为您可以使用:

Get-ADUser -Filter {enabled -eq "True"}

Another hint: ISE shouldn't be used for running scripts as it sometimes behave in strange ways (especially when you run out of memory on your machine). 另一个提示:不应将ISE用于运行脚本,因为它有时会以奇怪的方式运行(尤其是当计算机内存不足时)。 You should use powershell.exe instead. 您应该改用powershell.exe

Edit: to improve even more you could try to choose only the attributes you need using 编辑:要改善更多,您可以尝试仅选择需要使用的属性

$t = $allusers |select userprincipalname,enabled

And then use Where-Object to filter. 然后使用Where-Object进行过滤。 For comparison: 为了比较:

Measure-command {($allusers | where {$_.enabled -eq "True"}).count}

took two minutes when 花了两分钟

Measure-command {($t | where {$_.enabled -eq "True"}).count}

took two seconds (but selecting takes about 2 mins so the overall time is more or less the same). 花费了两秒钟的时间(但是选择大约需要2分钟,因此总体时间大致相同)。 However, this strongly depends on the scenario. 但是,这在很大程度上取决于方案。 I'll leave it to you so you can find the best solution for your case. 我将它留给您,以便您找到适合您情况的最佳解决方案。 Remember, Measure-Command is your very good friend for this! 记住, Measure-Command是您的好朋友!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM