首页 文章

如何在针对特定服务器时让Get-ADUser更好地运行?

提问于
浏览
2

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

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

另外,有没有办法知道 which 服务器如果我没有指定服务器,Get-ADuser会从中提取信息?

已连接服务器:* [服务器未指定] >>已用时间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
}

2 回答

  • 0

    你看过 Trace-Command 了吗?你可以使用 Get-TraceSource 获得一些关于如何减少噪音的想法,但作为一个起点,尝试这样的事情:

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

    你很可能会知道它挂在哪里 .

  • 0

    在阅读了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会提取其他属性,其中“Surname”就是其中之一(请参阅下面的链接) . 一旦我删除了对“sn”属性的请求,即使指定了服务器,也会在3秒内获得数据 . 然后,我测试了距离最远的服务器,从检索到的属性中删除“sn”后仅用了6秒,用42秒就可以了 .

    Select-Object -Property $propertyList
    

    我发现还有一个奇怪之处 . 当包含Select-Object语句时,当"sn"包含在属性列表中时,解析时间会上升 dramaticallyonly .

    我现在知道如何更准确地针对问题并解决它,但此时我没有具体的答案,为什么异常存在 . 希望这个问题可以帮助别人 .

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

相关问题