我有一项任务要从位于我们多域AD林中的几个组中的用户那里获取userPrincipalName属性。
问题是,我不能使用Select从获得用户的UPN,因为这个cmdlet只返回有限数量的属性(samaccountname、name、SID和DN),而UPN不是其中之一。
我编写了以下代码(获取“名称”,并通过"name“搜索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分钟(测量命令cmdlet)。我们有大量的用户跨越多个领域。
我的问题是如何改进我的代码以获得用户的UPN更快的更快的
我知道System.DirectoryServices.DirectorySearcher,但不知道如何用txt文件(“名称”列表)实现这个方法。
任何帮助都将不胜感激。
发布于 2017-10-29 23:13:58
实际上,您可以从一行代码中获得它。简单的..。:)
Get-ADGroupMember -Identity "group name" |%{get-aduser $_.SamAccountName | select userPrincipalName } > c:\scripts\upnofADgroup.txt发布于 2016-07-25 20:18:40
最快的方法可能是完全避免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是成员的数量)。
发布于 2016-07-26 12:20:33
好了,伙计们,我明白了:
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')https://stackoverflow.com/questions/38576120
复制相似问题