惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
L
LangChain Blog
美团技术团队
N
Netflix TechBlog - Medium
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
博客园 - 司徒正美
爱范儿
爱范儿
D
DataBreaches.Net
月光博客
月光博客
U
Unit 42
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
腾讯CDC

博客园 - 霖雨

用PowerShell命令一次启动多个前后端应用 Visual Studio Code中配置Cline 解决MSSQLLocalDB连接失败的问题 使用App Registration 访问 SharePoint Online (二) 使用App Registration 访问 SharePoint Online (一) SharePoint Online 文档库的还原功能 SharePoint Online 快捷方式功能 SharePoint Online 文档库的收藏功能 使用豆包开发SharePoint Framework项目 Power Automate 还原误删的 Flow PowerShell 获取已删除的 Automate Flow PowerShell 批量添加用户到组 PowerShell 批量下载 SharePoint Online 文档 PowerShell 清空 SharePoint Online 列表数据 PowerShell 新建 SharePoint Online 列表 PowerShell 获取 SharePoint Online 站点信息 PowerShell 启用 SharePoint Online CDN SharePoint Online 定时发布页面 SharePoint Online 页面诊断 Microsoft Whiteboard 的使用 SharePoint Online 网站配置时区 SharePoint Online 集成 Viva Connections SharePoint Online 分区模板 SharePoint Online 嵌入 Power BI 报表
PowerShell 新建 Address Book Policy
霖雨 · 2026-03-02 · via 博客园 - 霖雨

  前言

  最近在隔离用户

  正文

  1.ABP脚本的生成结果

image

  2.生成的命令

<#
.SYNOPSIS
为Exchange Online配置按域名隔离的地址簿策略(ABP),包含专属离线地址簿(OAB)
.DESCRIPTION
该脚本会为指定域名创建专属的GAL、地址列表、离线地址簿(OAB)和地址簿策略,并批量分配给该域名的所有用户
.AUTHOR
编程助手
.PARAMETER TargetDomain
必填参数,指定要隔离的目标域名(如contoso.com)
.PARAMETER AdminUPN
必填参数,Exchange Online管理员的UPN(如admin@contoso.com)
.PARAMETER PolicyPrefix
可选参数,策略命名前缀(默认值:ABP_)
.EXAMPLE
.\Configure-ABP-PerDomain.ps1 -TargetDomain "contoso.com" -AdminUPN "admin@contoso.com"
.EXAMPLE
.\Configure-ABP-PerDomain.ps1 -TargetDomain "fabrikam.com" -AdminUPN "admin@fabrikam.com" -PolicyPrefix "Fabrikam_"
#>

[CmdletBinding()]
param (
    [Parameter(Mandatory=$true)]
    [string]$TargetDomain,

    [Parameter(Mandatory=$true)]
    [string]$AdminUPN,

    [string]$PolicyPrefix = "ABP_"
)

# -------------------------- 初始化配置 --------------------------
# 定义所有策略/列表名称(自动拼接前缀+域名)
$addressListName = "$PolicyPrefix$TargetDomain Address List"
$galName = "$PolicyPrefix$TargetDomain GAL"
$roomAddressListName = "$PolicyPrefix$TargetDomain_Room_List"
$oabName = "$PolicyPrefix$TargetDomain OAB"
$abpName = "$PolicyPrefix$TargetDomain Policy"

# 设置错误处理
$ErrorActionPreference = "Stop"

try {
    Write-Host "galName" $galName
    # -------------------------- 1. 检查并安装Exchange Online模块 --------------------------
    Write-Host "`n[1/7]检查Exchange Online PowerShell模块..." -ForegroundColor Cyan
    if (-not (Get-Module -Name ExchangeOnlineManagement -ListAvailable)) {
        Write-Host "正在安装ExchangeOnlineManagement模块..." -ForegroundColor Yellow
        Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber -Scope CurrentUser
    }
    Import-Module ExchangeOnlineManagement -Force

    # -------------------------- 2. 连接Exchange Online --------------------------
    Write-Host "`n[2/7]连接Exchange Online..." -ForegroundColor Cyan
    Connect-ExchangeOnline -UserPrincipalName $AdminUPN -ShowProgress $true -ErrorAction Stop
    Write-Host "✅ 成功连接到Exchange Online" -ForegroundColor Green

    # -------------------------- 3. 创建专属地址列表 --------------------------
    Write-Host "`n[3/7]创建$TargetDomain专属地址列表..." -ForegroundColor Cyan
    # 检查地址列表是否已存在
    if (-not (Get-AddressList -Identity $addressListName -ErrorAction SilentlyContinue)) {
        New-AddressList -Name $addressListName `
            -RecipientFilter {RecipientType -eq 'UserMailbox' -and PrimarySmtpAddress -like "*yu@$TargetDomain"} `
            #-IncludedRecipients MailboxUsers
        Write-Host "✅ 成功创建地址列表: $addressListName" -ForegroundColor Green
    }
    else {
        Write-Host "⚠️  地址列表$addressListName已存在,跳过创建" -ForegroundColor Yellow
    }

    # -------------------------- 4. 创建专属全局地址列表(GAL) --------------------------
    Write-Host "`n[4/7]创建$TargetDomain专属GAL..." -ForegroundColor Cyan
    Write-Host "GAL $galName"
    # 检查GAL是否已存在
    if (-not (Get-GlobalAddressList -Identity $galName -ErrorAction SilentlyContinue)) {
        New-GlobalAddressList -Name $galName `
            -RecipientFilter {RecipientType -eq 'UserMailbox' -and PrimarySmtpAddress -like "*yu@$TargetDomain"}
        Write-Host "✅ 成功创建GAL: $galName" -ForegroundColor Green
    }
    else {
        Write-Host "⚠️  GAL $galName已存在,跳过创建" -ForegroundColor Yellow
    }
    

    # --------------------------5. 创建会议室邮箱地址列表(ABP的RoomList所需)--------------------------
    Write-Host "`n[5/7] 创建$TargetDomain会议室地址列表..." -ForegroundColor Cyan
    if (-not (Get-AddressList -Identity $roomAddressListName -ErrorAction SilentlyContinue)) {
        New-AddressList -Name $roomAddressListName `
            -RecipientFilter {RecipientDisplayType -eq 'ConferenceRoomMailbox' -and PrimarySmtpAddress -like "*yu@$TargetDomain"}
        Write-Host "✅ 创建会议室地址列表: $roomAddressListName" -ForegroundColor Green
    } else { Write-Host "⚠️  会议室地址列表已存在" -ForegroundColor Yellow }
    $abpRoomList = (Get-AddressList -Identity $roomAddressListName).Identity

    # -------------------------- 6. 创建专属离线地址簿(OAB) --------------------------
    Write-Host "`n[6/7]创建地址簿策略..." -ForegroundColor Cyan
    $defaultOAB = Get-OfflineAddressBook | Select-Object -First 1
    if (-not (Get-AddressBookPolicy -Identity $abpName -ErrorAction SilentlyContinue)) {
        New-AddressBookPolicy -Name $abpName `
            -GlobalAddressList $galName `
            -AddressLists $addressListName `
            -OfflineAddressBook $defaultOAB.Identity `
        -RoomList $abpRoomList
        Write-Host "✅ 成功创建地址簿策略: $abpName" -ForegroundColor Green
    }
    else {
        Write-Host "⚠️  地址簿策略$abpName已存在,更新配置" -ForegroundColor Yellow
        Set-AddressBookPolicy -Identity $abpName `
            -GlobalAddressList $galName `
            -AddressLists $addressListName `
            -OfflineAddressBook $defaultOAB.Identity
        Write-Host "✅ 已更新ABP配置: $abpName" -ForegroundColor Green
    }

    # -------------------------- 7. 批量分配策略给目标域名用户 --------------------------
    Write-Host "`n[6/7]批量分配策略给$TargetDomain的所有用户..." -ForegroundColor Cyan
    $users = Get-Mailbox -ResultSize Unlimited | Where-Object {$_.PrimarySmtpAddress -like "*yu@$TargetDomain"}
    
    if ($users.Count -eq 0) {
        Write-Host "⚠️  未找到$TargetDomain域名的任何邮箱用户" -ForegroundColor Yellow
    }
    else {
        $users | Set-Mailbox -AddressBookPolicy $abpName
        Write-Host "✅ 已为$($users.Count)个用户分配地址簿策略: $abpName" -ForegroundColor Green

        # 验证分配结果(显示前5个用户)
        Write-Host "`n📌 验证结果(前5个用户):" -ForegroundColor Cyan
        $users | Select-Object -First 5 Name, PrimarySmtpAddress, AddressBookPolicy | Format-Table -AutoSize
    }

    Write-Host "`n🎉 所有操作执行完成!" -ForegroundColor Green
    Write-Host "📢 注意:OAB和ABP生效可能需要1-2小时,建议用户:" -ForegroundColor Cyan
    Write-Host "   1. 重启Outlook客户端" -ForegroundColor Cyan
    Write-Host "   2. 手动下载OAB(Outlook → 文件 → 账户设置 → 下载地址簿)" -ForegroundColor Cyan
}
catch {
    Write-Host "`n❌ 执行过程中出错: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}
finally {
    # 断开Exchange Online连接
    if (Get-PSSession | Where-Object {$_.ConfigurationName -eq 'Microsoft.Exchange'}) {
        Write-Host "`n🔌 断开Exchange Online连接..." -ForegroundColor Cyan
        Disconnect-ExchangeOnline -Confirm:$false
    }
}

  结束语

  搞了好久,有点困了

posted @ 2026-03-02 22:59  霖雨  阅读(13)  评论()    收藏  举报