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

使用PowerShell导入M365用户失败:UserPrincipalName相关问题排查

解决PowerShell导入Microsoft 365用户时的UserPrincipalName错误

针对你遇到的UPN相关导入错误,给你列几个核心排查方向:

  • 校验数据源的UPN格式
    确保每条用户数据的UPN都符合规范:要么是用户名@租户域名.onmicrosoft.com,要么是租户已验证的自定义域名后缀,不能包含空格、特殊字符(仅允许.、_、-),也不能是空值。可以先用PowerShell快速筛选无效行:

    # 导入CSV数据源并检查UPN格式
    $userList = Import-Csv -Path "你的用户文件路径.csv"
    $invalidUPNs = $userList | Where-Object { 
        ![string]::IsNullOrEmpty($_.UserPrincipalName) -and 
        $_.UserPrincipalName -notmatch '^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    }
    $invalidUPNs # 输出格式错误的用户数据,修正后再导入
    
  • 确认变量引用的上下文匹配
    如果你用循环处理数据,要确保变量引用的属性和数据源的列名完全一致:

    • 若CSV列名是UserPrincipalName,则用$_.UserPrincipalName
    • 若CSV列名是UPN,则用$_.UPN
      可以在循环里先输出实际值验证:
    $userList | ForEach-Object {
        Write-Host "当前用户UPN:$($_.UserPrincipalName)" # 确认输出的是有效值
        # 执行导入命令
        New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName ...
    }
    
  • 清理空值或隐藏字符
    部分数据源的UPN列可能存在空格、不可见字符,导致cmdlet识别为空值。可以先做清理处理:

    $userList | ForEach-Object {
        $cleanUPN = $_.UserPrincipalName.Trim()
        if ([string]::IsNullOrEmpty($cleanUPN)) {
            Write-Warning "跳过空UPN用户:$($_.DisplayName)"
            return
        }
        New-MsolUser -UserPrincipalName $cleanUPN ...
    }
    
  • 验证租户域名有效性
    若使用自定义域名作为UPN后缀,必须先在Microsoft 365管理中心完成域名验证。可以用命令查看已验证域名:

    Get-MsolDomain | Where-Object { $_.Status -eq "Verified" }
    

    确保UPN后缀属于上述已验证域名列表。

内容的提问来源于stack exchange,提问作者Cas Beute

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 00:59:50