2016-07-25 20 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