gpt4 book ai didi

powershell - Get-ADUser 不缓存结果

转载 作者:行者123 更新时间:2023-12-02 22:48:31 29 4
gpt4 key购买 nike

$users = get-aduser -filter 'extensionAttribute10 -notlike "*" -and proxyaddresses -like "*"' -Properties extensionAttribute10,proxyaddresses,company

foreach($user in $users){
$user
}

在上面的示例中,get-aduser commandlet 返回大约 20,000 个对象(并且需要一些时间来运行)。一旦 Get-ADUser 完成并且脚本进入 foreach 循环,PowerShell 似乎会为数组中的每个对象再次调用 Get-ADUser 而不是仅仅返回值变量(随后真的非常慢)。

如果我使用 $users[100] 引用数组中的对象,则同样的行为适用 - 第一次返回用户很慢,因为它调用域 Controller ,第二次我调用它结果被缓存后立即返回。

这是预期的行为吗,有没有办法控制它/预先缓存所有结果?

PS版本:5.1.15063.1563

Update - It seems this only occurs when you're querying objects in a forest which is remote to the user executing the command:

$myForest | Get-ADUser -filter * 
$myForest[0] # <-- this doesn't reach back to a DC to return the user

$remoteForest | Get-ADUser -filter * -server dc1.remoteforest.com
$remoteForest[0] # <-- this will call back to a DC to fetch the user even though it's
# been successfully retrieved in the previous line

最佳答案

当在数组中引用结果时,我没有观察到 ActiveDirectory 模块调用 Get-ADUser。您可能遇到了内存问题。我建议检查 PowerShell 的内存分配设置,并与脚本执行时的实际内存利用率进行比较。

#Start the Win RM service because it is required for the subsequent command.
Get-Service -Name WinRM | Start-Service

#Get the Max Memory Per Shell in MB
$memLimitPerShellMB = Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB

#Write value to console.
$memLimitPerShellMB.Value

#Check PowerShell memory utilization. Capture this information in a separate shell while your other, heavier, process is running.
get-process | Where-Object ProcessName -like "*PowerShell*"

#If needed, the MaxMemoryPerShellMB value can be adjusted using this command:
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB <NEW VALUE>

关于powershell - Get-ADUser 不缓存结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54790231/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com