【问题标题】:How to get Get-ADUser to perform better when targeting a specific server?以特定服务器为目标时,如何让 Get-ADUser 表现更好?
【发布时间】:2019-04-06 03:20:42
【问题描述】:

我正在从域中的所有用户那里获取一组属性。如果我没有指定特定的域控制器,查询会在不到一秒的时间内返回有效结果。如果我指定一个目标控制器,即使在我最近的域控制器上,结果也需要 18 秒才能返回。唯一的区别是我使用-Server $serverName 定位服务器。

如何在指定服务器时获得相同的性能?这是用户界面驱动的,因此在数据更改后等待 18 秒(最少)是很长的等待时间。在函数$serverName 中提取存储的字符串值,因此不执行任何处理。

另外,如果我不指定服务器,有没有办法知道哪个服务器Get-ADuser 提取了它的信息?

Connected-Server: * [Server not specified] >> Elapsed time HH:MM:SS = 00:00:00.9451947

连接的服务器:SERVER1 >> 经过时间 HH:MM:SS = 00:00:42.8815911

连接的服务器:SERVER2 >> 经过时间 HH:MM:SS = 00:00:39.8800249

连接的服务器:SERVER3 >> 经过时间 HH:MM:SS = 00:00:18.1686541

Function Get-TargetObjectList( $targetSearchBase )
{
    $propertyList  = "Enabled", "DistinguishedName", "Name", "department", "givenName","sn","displayName","CanonicalName", "Description"
    $serverName    = Get-CurrentDC  # which domain controller name did the user choose from the drop down list?

    # if $serverName is “*” then do not target a specific server

    if ($serverName -eq "*")
    {
        $tempObjects   = Get-ADUser -Filter * -Properties $propertyList -SearchBase $targetSearchBase
    } else {   
        $tempObjects   = Get-ADUser -Filter * -Properties $propertyList -Server $serverName -SearchBase $targetSearchBase
    }

    Write-Host "Get-TargetObjectList: " $serverName
    $targetObjects = $tempObjects | Select-Object -Property $propertyList | Sort-Object -Property Name
    return $targetObjects
}

【问题讨论】:

  • 所以我无法帮助您解决您所看到的性能问题,除了询问您何时在未指定服务器的情况下运行它,是您在会话期间运行的第一个 AD 相关命令还是其他命令以前运行过吗?如果它不是您会话中的第一个 AD 命令,则您可能有一个现有的连接并且这就是正在使用的连接,而如果您提供 -server 一个新的连接正在建立。除此之外,环境变量$env:LogonServer 至少应该能够告诉您默认使用哪个 DC。

标签: powershell active-directory


【解决方案1】:

你看过Trace-Command吗?您可以使用Get-TraceSource 来了解如何减少一些噪音,但作为起点,请尝试以下方法:

Trace-Command -Name "*" -Expression { Get-ADUser -Filter * -Properties $propertyList -Server $serverName -SearchBase $targetSearchBase }

您可能会知道它挂在哪里。

【讨论】:

  • 我开始按照您的建议使用 Trace-Command。虽然它没有直接让我得出结论,但它确实让我开始专注于严格的要点。
【解决方案2】:

在阅读 Mike Garuccio 和 Andy Simmons 的回答后,我决定剥离除以下内容之外的所有内容,并查看一个服务器,这是我最近的服务器。 我遇到了可重现的奇怪事件组合。

$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()

$targetSearchBase = "OU=User Accounts,DC=XXX,DC=XXX,DC=com"
$propertyList  = "Enabled", "DistinguishedName", "Name", "department", "givenName","displayName","CanonicalName", "Description"
#$propertyList  = "Enabled", "DistinguishedName", "Name", "department", "givenName","sn","displayName","CanonicalName", "Description"
$serverName = "TARGET_SERVER_NAME"

$tempObjects = Get-ADUser -Filter * -Properties $propertyList -Server $serverName -SearchBase $targetSearchBase

#server not specified
#$tempObjects = Get-ADUser -Filter * -Properties $propertyList -SearchBase $targetSearchBase
$targetObjects = $tempObjects | Select-Object -Property $propertyList | Sort-Object -Property Name

$stopwatch.Stop()
$elapsedTime = $stopwatch.Elapsed
$elapsed = "Elapsed time HH:MM:SS = $($elapsedTime)"
Write-Host "Server: " $serverName
Write-Host "server data retrieved: " $elapsed

如果我不指定服务器,则在不到 1 秒的时间内检索到列表

 $tempObjects = Get-ADUser -Filter * -Properties $propertyList -SearchBase $targetSearchBase

如果我确实指定了离我最近的服务器,则会在 18 秒内检索到列表

 $tempObjects = Get-ADUser -Filter * -Properties $propertyList -Server $serverName -SearchBase $targetSearchBase

查看所有对象属性后,我注意到该命令检索了属性“Surname”和“sn”,尽管我只选择了“sn”。

 $tempObjects | Get-Member 

这是因为 Get-ADUser 默认会提取其他属性,其中“姓氏”是其中之一(请参阅下面的链接)。一旦我删除了对“sn”属性的请求,即使指定了服务器,也可以在 3 秒内获得数据。然后我测试了距离最远的服务器,从检索到的属性中删除“sn”后只用了 6 秒,而使用它只用了 42 秒。

 Select-Object -Property $propertyList

我发现了另一个奇怪的地方。当包含 Select-Object 语句时,解析时间显着增加,但在属性列表中包含“sn”时。

我现在知道如何更准确地定位问题并可以解决它,但目前我对异常存在的原因没有具体的答案。希望这个问题对其他人有所帮助。

http://social.technet.microsoft.com/wiki/contents/articles/12037.active-directory-get-aduser-default-and-extended-properties.aspx

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多