我有一個任務可以獲取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文件(「名稱」列表)實現此方法。
任何幫助將不勝感激。
謝謝您的回覆,馬蒂亞斯 我提高你的例子一點點,因爲它不能正常工作: 的foreach($集團在$組){$ 會員= Get-ADObject -LDAPFilter「(memberOf = $($ Group.DistinguishedName))」-Server「GC:3268」-Properties UserPrincipalName Write-Host $成員 $成員|選擇對象UserPrincipalName |加入路徑$ ScriptPath'upnOfSDFC_AD_GroupsMembers.txt')} } } } 而最後一個字符串(Select-Object UPN)是運行速度太慢。現在的問題是如何提高**這個**字符串 –