2016-07-25 80 views
0

我有一个任务可以获取userPrincipalName属性,这些属性来自我们的多域AD森林中多个组中的用户。从Get-ADGroupMember获取UPN

问题是我无法使用Select-Object从Get-ADGroupMember获取用户的UPN,因为此cmdlet只返回有限数量的属性(samaccountname,name,SID和DN),并且UPN不是其中之一。

我写了这个代码(得到“名”和比“名”搜索UPN):

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path 
    $LocalSite = (Get-ADDomainController -Discover).Site 
    $NewTargetGC = Get-ADDomainController -Discover -Service 6 -SiteName 
    $LocalSite 
    IF (!$NewTargetGC) 
    { $NewTargetGC = Get-ADDomainController -Discover -Service 6 -NextClosestSite } 
    $NewTargetGCHostName = $NewTargetGC.HostName 
    $LocalGC = “$NewTargetGCHostName” + “:3268” 

    $domains = (Get-ADForest).domains 
    $MembersOfSFDC_Groups = foreach ($domain in $domains) { 
    $Group = Get-ADGroup -Filter { Name -like "*groupname*" } -Server $Domain 
    $Group | Get-ADGroupMember -Server $domain | Select @{ 
    Name="Domain";Expression={$Domain}},@{ 
    Name="Group";Expression={$Group.Name}}, name} 

    $DisplayNames = $MembersOfSFDC_Groups.name 
    $DisplayNames |Out-file (Join-Path $ScriptPath 'DisplayNames.txt') 

    Get-content (Join-Path $ScriptPath 'DisplayNames.txt') | 
    $displaynames | ForEach-Object { 
    Get-ADUser -Server $LocalGC -Filter {Name -eq $_} | 
    Select-Object -Property userPrincipalName} | 
    Out-File (Join-Path $ScriptPath 'upnOfSDFC_AD_GroupsMembers.txt') 

但接下来的问题是,这个代码运行约30分钟(测量-Command cmdlet的)。我们在多个域中拥有大量的用户。

我的问题是如何改进我的代码让用户的UPN更快更快

我知道System.DirectoryServices.DirectorySearcher,但不知道如何用我的txt文件(“名称”列表)实现此方法。

任何帮助将不胜感激。

回答

0

最快的方法可能是避免Get-ADGroupMember干脆,只是搜索组,然后寻找属于该组的对象:

$Group = Get-ADGroup -Filter { Name -like "*groupname*" } -Server $Domain 
$Members = Get-ADObject -LDAPFilter "(memberOf=$($Group.DistinguishedName))" -Properties UserPrincipalName 
$Members |Select-Object UserPrincipalName |Out-File (Join-Path $ScriptPath 'upnOfSDFC_AD_GroupsMembers.txt') 

现在你到2个查询,而不是2 + N(N为成员的数量)

+0

谢谢您的回复,马蒂亚斯 我提高你的例子一点点,因为它不能正常工作: 的foreach($集团在$组){$ 会员= Get-ADObject -LDAPFilter“(memberOf = $($ Group.DistinguishedName))”-Server“GC:3268”-Properties UserPrincipalName Write-Host $成员 $成员|选择对象UserPrincipalName |加入路径$ ScriptPath'upnOfSDFC_AD_GroupsMembers.txt')} } } } 而最后一个字符串(Select-Object UPN)是运行速度太慢。现在的问题是如何提高**这个**字符串 –

0

好吧,伙计们,心中已经明白了:

function Get-DomainFromDN ($param) 
    { 
    $dn1 = $param -split "," | ? {$_ -like "DC=*"} 
    $dn2 = $dn1 -join "." -replace ("DC=", "") 
    $script:test = $dn2 
    return $dn2 
    } 

foreach ($Group in $Groups) { 
$Members = Get-ADObject -LDAPFilter "(&(objectCategory=user)(memberOf=$($Group.DistinguishedName)))" -Properties UserPrincipalName -Server (Get-DomainFromDN ($group.DistinguishedName)) 
$UPN_Of_SFDC_Groups += $Members |Select-Object UserPrincipalName } 

$UPN_Of_SFDC_Groups | Out-file (Join-Path $ScriptPath 'upnOfSDFC_AD_GroupsMembers.txt') 
0

实际上,你可以从一行代码得到它。 Simples ... :)

Get-ADGroupMember -Identity "group name" |%{get-aduser $_.SamAccountName | select userPrincipalName } > c:\scripts\upnofADgroup.txt 
+0

这并没有提供一个问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/17776125) – TheIncorrigible1