gpt4 book ai didi

powershell - 获取 ADUser : The server has returned the following error: invalid enumeration context

转载 作者:行者123 更新时间:2023-12-02 01:51:37 24 4
gpt4 key购买 nike

我正在尝试从我们的广告中获取所有承包商和员工,并启用 true 过滤器。 AD本身非常庞大,数据量很大,在运行powershell脚本时可能会导致超时限制。当我将其放入变量中时,我收到此错误,如果我直接导​​出,则会收到超时限制。

有没有什么方法可以收集所有承包商和员工数据以及具有大广告的启用员工,而不会出现此问题?我得到了一个输出,但不确定输出是否可靠,因为我收到了错误。

谢谢

Param(
[Parameter(Mandatory = $true,ValueFromPipeline = $false,HelpMessage = "Specify a output file name and path.")]
[string]
$OutputFileNamePath = $null
)
$Storage = @()
Write-Host "**********************************************************" -ForegroundColor Green
Write-Host "* On Process *" -ForegroundColor Green
Write-Host "**********************************************************" -ForegroundColor Green
$filter = @("CONTRACTOR", "EMPLOYEE")
$Storage = Get-ADUser -Filter * -Properties EmployeeNumber,Name,SamAccountName,employeeType,PasswordLastSet,LastLogonDate | Where {($_.Enabled -eq $True) -and $filter -contains $_.employeeType} | Select EmployeeNumber,Name,SamAccountName,employeeType,PasswordLastSet,LastLogonDate
$Storage | Export-Csv -Path $OutputFileNamePath".csv" -Force
Write-Host "**********************************************************" -ForegroundColor Green
Write-Host "* Done *" -ForegroundColor Green
Write-Host "**********************************************************" -ForegroundColor Green

最佳答案

This TechNet article向我们提示为什么会发生此错误,基本上是:

  1. Get-ADUser uses paging (256 object per page by default)
  2. It is up to the client to request new Pages
  3. When piping out AD Objects, the longer the code down the pipeline takes to process, the slower we retrieve data from Active Directory Web Services
  4. If that slower processing causes the retrieval time to run over 30 minutes, then the Enumeration Context Expires

很可能,您的查询已经运行了 30 分钟,导致了此异常。解决方案是进行更有效的查询,以便它在此时间之前完成,或者,MS 不推荐:

Increase the "MaxEnumContextExpiration" value for Active Directory Web Service. There are many reasons not to take this approach.


您可以利用 LDAPFilter 来提高查询性能,在这种情况下不需要使用 Where-Object:

$properties = 'EmployeeNumber', 'employeeType', 'PasswordLastSet', 'LastLogonDate'
$filter = '(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(employeeType=CONTRACTOR)(employeeType=EMPLOYEE)))'

Get-ADUser -LDAPFilter $filter -Properties $properties

将 LDAP 过滤器转换为可读的内容:

  • & - 逻辑AND运算符
  • | - 逻辑OR运算符
  • (!userAccountControl:1.2.840.113556.1.4.803:=2) - 启用的用户对象
  • (|(employeeType=CONTRACTOR)(employeeType=EMPLOYEE)) - employeeType 为“承包商”OR “员工”
  • (&(...)(|(...)(...))) - 必须满足所有条款。我们可以将其解读为:
    ( 用户已启用 ) AND ( 用户的 employeeType 是“承包商”OR“员工”)

供将来引用Active Directory: LDAP Syntax Filters

关于powershell - 获取 ADUser : The server has returned the following error: invalid enumeration context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70264166/

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