You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell和正则表达式格式化AD用户电话与手机号

问题修复:AD用户电话号码格式转换脚本

核心问题分析

你的脚本当前存在三个关键问题导致正则不生效:

  • 座机和手机号使用了相同的正则表达式,但两者的目标拆分规则完全不同
  • 将字符串替换逻辑放在了{}脚本块中,Set-ADUser无法正确解析这种格式的参数值
  • 未处理号码中可能存在的非数字字符(如空格、括号),导致正则匹配失败

修改后的完整脚本

function Get-CountryCode ($CountryCode) {
    switch ($CountryCode) {
        "UK" { "+44" }
        "US" { "+1" }
        default { "" }
    }
}

$paramGetADUser = @{
    Filter      = "(Enabled -eq 'True') -and (Mail -like '*') -and ( (OfficePhone -like '*') -or (MobilePhone -like '*') )"
    Properties  = 'EmailAddress', 'Mobilephone', 'OfficePhone', 'Country'
    SearchBase  = 'OU=Users,DC=Domain,DC=com'
    SearchScope = 'Subtree'
}

$ADUsers = Get-ADUser @paramGetADUser |
    Select-Object Name, EmailAddress, UserPrincipalName, SamAccountName, Mobilephone, OfficePhone, Country

foreach ($ADuser in $ADUsers) {
    # 仅处理目标地区用户
    $countryCode = Get-CountryCode -CountryCode $ADuser.Country
    if (-not $countryCode) {
        Write-Host "跳过非目标地区用户: $($ADuser.Name) (国家: $($ADuser.Country))" -ForegroundColor Cyan
        continue
    }

    if ($ADuser.MobilePhone) {
        Write-Host "转换 '$($ADuser.Name)' 的手机号: '$($ADuser.MobilePhone)' ..." -ForegroundColor Yellow
        
        # 第一步:清理号码,提取纯数字
        $cleanMobile = $ADuser.MobilePhone -replace '\D', ''
        # 第二步:匹配英国手机号格式(原格式0开头10位),转换为+44 xxx xxx xxx
        $formattedMobile = $cleanMobile -replace '^0(\d{3})(\d{3})(\d{3})', "$countryCode `$1 `$2 `$3"

        if ($formattedMobile -ne $cleanMobile) {
            $paramSetADUserMobile = @{
                Identity    = $ADuser.SamAccountName
                MobilePhone = $formattedMobile
                ErrorAction = 'Stop'
                Verbose     = $true
            }
            try {
                Set-ADUser @paramSetADUserMobile
                Write-Host "`t转换完成: '$formattedMobile'" -ForegroundColor Green
            }
            catch {
                Write-Host "`t转换失败: $($_.Exception.Message)" -ForegroundColor Red
            }
        }
        else {
            Write-Host "`t号码格式不符合规则,跳过" -ForegroundColor Gray
        }
    }

    if ($ADuser.OfficePhone) {
        Write-Host "转换 '$($ADuser.Name)' 的座机号: '$($ADuser.OfficePhone)' ..." -ForegroundColor Yellow
        
        # 第一步:清理号码,提取纯数字
        $cleanOffice = $ADuser.OfficePhone -replace '\D', ''
        # 第二步:匹配英国座机格式(原格式0开头10位),转换为+44 x xxxx xxxx
        $formattedOffice = $cleanOffice -replace '^0(\d)(\d{4})(\d{4})', "$countryCode `$1 `$2 `$3"

        if ($formattedOffice -ne $cleanOffice) {
            $paramSetADUserPhone = @{
                Identity    = $ADuser.SamAccountName
                OfficePhone = $formattedOffice
                ErrorAction = 'Stop'
                Verbose     = $true
            }
            try {
                Set-ADUser @paramSetADUserPhone
                Write-Host "`t转换完成: '$formattedOffice'" -ForegroundColor Green
            }
            catch {
                Write-Host "`t转换失败: $($_.Exception.Message)" -ForegroundColor Red
            }
        }
        else {
            Write-Host "`t号码格式不符合规则,跳过" -ForegroundColor Gray
        }
    }
}

关键修改说明

  • 拆分正则规则:针对座机和手机号的不同格式需求,分别编写匹配规则,确保转换后符合英国地区号码规范
  • 移除脚本块赋值:直接计算格式化后的字符串并赋值给参数,避免Set-ADUser解析错误
  • 增加号码清理步骤:通过-replace '\D', ''移除所有非数字字符,确保正则能正确匹配原始号码
  • 添加错误处理逻辑:用try/catch包裹更新操作,便于排查转换失败的具体原因
  • 增加格式校验:对比格式化前后的号码,仅当匹配成功时执行更新,避免无效修改

内容的提问来源于stack exchange,提问作者Senior Systems Engineer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:50:09