AD 中的组,将包括具有邮寄地址的所有用户

Group in AD, which will include all users who have a mailing address

提问人:Norbert Yuhas 提问时间:5/18/2023 更新时间:5/22/2023 访问量:38

问:

我需要在 AD 中创建一个组,该组将包括在我们的 Exchange Server 上拥有邮寄地址的所有用户。

如何选择此类用户并将其添加到组中? 我想定期运行这样的脚本(例如,每周一次)以保持最新状态。

谢谢

PowerShell Active Directory Exchange-Server

评论


答:

1赞 Michael 5/22/2023 #1

我可以想到 Exchange 管理程序、纯 AD Powershell 和 Exchange 动态通讯组,如果您只需要它用于邮件列表目的。

这是前面提到的前 2 种方法。

Exchange 命令行管理程序版本(假定已知如何连接到 EMS):

# Set the AD group name
$ADGroupName = "GroupName"  # Replace with the desired name for the AD group

# Connect to the Exchange Server
# <connection code here>

# Get the users with mailing addresses
$UsersWithMailingAddresses = Get-Mailbox | Where-Object { $_.EmailAddresses -like "*SMTP:*" }
    
# Create the AD group
# Skip this step if Group is already present
#New-ADGroup -Name $ADGroupName -GroupCategory Security -GroupScope Global -DisplayName $ADGroupName -Path "OU=Groups,DC=YourDomain,DC=com"

# Add users to the AD group
foreach ($User in $UsersWithMailingAddresses) {
        Add-ADGroupMember -Identity $ADGroupName -Members $User.SamAccountName
}

# Close the Exchange Server session
# Remove-PSSession $Session

AD PowerShell 模块版本:

# Set the AD group name
$ADGroupName = "GroupName"  # Replace with the desired name for the AD group

# Set the attribute to filter users
$Attribute = "mail"

# Set the filter value
$FilterValue = "*"

# Get the users with the specified attribute
$UsersWithAttribute = Get-ADUser -Filter "$Attribute -like '$FilterValue'"

# Create the AD group
# Skip this step if Group is already present
#New-ADGroup -Name $ADGroupName -GroupCategory Security -GroupScope Global -DisplayName $ADGroupName -Path "OU=Groups,DC=YourDomain,DC=com"

# Add users to the AD group
foreach ($User in $UsersWithAttribute) {
    Add-ADGroupMember -Identity $ADGroupName -Members $User.SamAccountName
}