$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]引用数组中的对象,则也会出现相同的行为-第一次调用用户调用域控制器时,返回用户的速度很慢,第二次调用它缓存结果后立即返回。
这是预期的行为吗,有没有办法控制它/将所有结果预先缓存?
PSVersion: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
答案 0 :(得分:0)
在数组中引用结果时,我还没有观察到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>