[英]Filtering multiple null values from Get-ADUser
I'm still working on learning powershell. I need to pull email and manager email from AD accounts in a group but exclude any records that have manager blank or email blank.我仍在努力学习 powershell。我需要从组中的 AD 帐户中提取 email 和经理 email,但排除任何经理空白或 email 空白的记录。 I don't think I can 'where' for two where conditions, so I can get one or the other.我不认为我可以在两个条件下“在哪里”,所以我可以得到一个或另一个。
Get-ADGroupMember -Identity "groupname" -Recursive | Get-ADUser -properties * | where manager -NE $null | select displayname, EmailAddress, @{Name="ManagerEmail";Expression={(Get-ADUser -property Emailaddress $_.manager).emailaddress}} | export-csv -path c:\data.csv -NoTypeInformation
How would I go about getting both filtered out?我 go 如何将两者都过滤掉?
You can use and should use the Active Directory Filter or LDAP Filter for this instead of doing the filtering with powershell :您可以并且应该为此使用Active Directory 过滤器或LDAP 过滤器,而不是使用powershell进行过滤:
$group = (Get-ADGroup groupname).DistinguishedName
$managerMap = @{}
$params = @{
LDAPFilter = "(&(memberof:1.2.840.113556.1.4.1941:=$group)(mail=*)(manager=*))"
Properties = 'mail', 'manager'
}
Get-ADUser @params | ForEach-Object {
if(-not $managerMap.ContainsKey($_.manager)) {
$managerMap[$_.manager] = (Get-ADUser $_.manager -Properties mail).mail
}
[pscustomobject]@{
DisplayName = $_.DisplayName
EmailAddress = $_.mail
ManagerEmail = $managerMap[$_.manager]
}
} | Export-Csv 'c:\data.csv' -NoTypeInformation
Details of the LDAP Filter: LDAP过滤器详情:
(& # AND, all conditions must be met
(memberof:1.2.840.113556.1.4.1941:=$group) # user is a member of `$group` (recursively)
(mail=*) # mail attribute is not null
(manager=*) # manager attribute is not null
) # closing AND
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.