【问题标题】:Get-ADUser not caching resultsGet-ADUser 不缓存结果
【发布时间】:2019-02-20 15:47:52
【问题描述】:
$users = get-aduser -filter 'extensionAttribute10 -notlike "*" -and proxyaddresses -like "*"' -Properties extensionAttribute10,proxyaddresses,company

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

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

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

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

PS版本:5.1.15063.1563

更新 - 似乎这只发生在您在执行命令的用户远程的森林中查询对象时:

$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

【问题讨论】:

  • 向我们展示你在循环中实际做了什么。简单地发射具有三个选定属性(+默认属性)的对象不会导致任何延迟。
  • 在上面的示例中,我仍然看到了延迟,我故意剥离了循环中的所有内容,以防是导致延迟的循环。我什至在访问数组中的一个项目时运行了 procmon,它调用了域控制器。我开始认为这可能是某种环境问题...
  • 这里有同样的问题。显然 PS 不会缓存来自远程林的非默认属性。如果你不指定属性,它会缓存它。
  • 你可以用get-aduser | foreach-object { $_ }节省内存

标签: powershell


【解决方案1】:

我没有观察到在数组中引用结果时调用 Get-ADUser 的 ActiveDirectory 模块。您可能遇到了内存问题。我建议检查 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多