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

求助:修改PowerShell脚本以导出OU内用户邮箱信息

解决方案

修改后的PowerShell脚本如下,已添加用户邮箱字段的获取逻辑:

$ou = 'distinguishedName of the OU'
Get-ADGroup -Filter * -SearchBase $ou -Properties Description | ForEach-Object {
    $currentGroup = $_
    # 获取组内成员并仅筛选用户对象
    Get-ADGroupMember $currentGroup | Where-Object { $_.ObjectClass -eq 'user' } | ForEach-Object {
        # 获取用户的mail属性
        $user = Get-ADUser $_.SamAccountName -Properties mail
        [pscustomobject]@{
            SamAccountName = $user.SamAccountName
            Name           = $user.Name
            GroupName      = $currentGroup.Name
            Description    = $currentGroup.Description
            Email          = $user.mail ?? '无邮箱' # 处理邮箱为空的情况,可选
        }
    }
} | Export-Csv C:\Users\MyUsername\Desktop\NameOfMyFile.csv -NoTypeInformation -Encoding UTF8

关键修改说明:

  • 添加Where-Object { $_.ObjectClass -eq 'user' }:过滤组内的非用户成员(比如嵌套组),避免对非用户对象调用Get-ADUser报错。
  • 使用Get-ADUser $_.SamAccountName -Properties mail:显式获取用户的mail属性(默认Get-ADUser不返回该属性)。
  • 新增Email字段,并通过?? '无邮箱'处理用户未配置邮箱的场景,可根据需求移除或修改默认值。
  • 添加-Encoding UTF8参数:确保导出的CSV文件中文显示正常,避免乱码。

内容的提问来源于stack exchange,提问作者S.Mahmood

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 13:30:54