【问题标题】:Retrieve Emails addresses in CSV file in Powershell via Get-ADGroup command通过 Get-ADGroup 命令在 Powershell 中检索 CSV 文件中的电子邮件地址
【发布时间】:2023-02-22 21:59:35
【问题描述】:

目前我有一个脚本,我可以查询一个 OU(我在脚本中定义),它将用户名、用户名、AD 组名称和该 AD 组的描述显示到一个 CSV 文件中:

$ou = 'distinguishedName of my OU'
Get-ADGroup -Filter * -SearchBase $ou -Properties Description | ForEach-Object {
    foreach($member in Get-ADGroupMember $_) {
        [pscustomobject]@{
            SamAccountName = $member.SamAccountName
            Name           = $member.Name
            GroupName      = $_.Name
            Description    = $_.Description
        }
    }
} | Export-csv C:\Users\Me\Desktop\MyFile.csv -NoTypeInformation

当我尝试在同一个脚本中提取用户的电子邮件地址时,出现错误。

$ou = 'distinguishedName of my OU'
Get-ADGroup -Filter * -SearchBase $ou -Properties 'Description', 'EmailAddress' | ForEach-Object {
    foreach($member in Get-ADGroupMember $_)  
    {
        [pscustomobject]@{
            SamAccountName = $member.SamAccountName
            Name           = $member.Name
            EmailAddress   = $_.EmailAddress
            GroupName      = $_.Name
            Description    = $_.Description
        }
    }
} | Export-csv C:\Users\Me\Desktop\MyFile.csv -NoTypeInformation

错误消息指出脚本在脚本的这一点附近失败:

-Properties 'Description', 'EmailAddress'

【问题讨论】:

  • 请向我们展示完整的确切错误消息:)

标签: powershell automation email-address get-aduser


【解决方案1】:

the E-mail-Address attribute in Active Directory 的 LDAP 显示名称不是 EmailAddress,而是 mail

Get-ADGroup -Filter * -SearchBase $ou -Properties 'Description', 'mail' | ...

【讨论】:

    【解决方案2】:

    如果您想包括用户您需要更进一步,为群组中的每个成员致电Get-ADUser
    问题是 Get-ADGroupMember 不仅可以返回用户,还可以返回计算机广告组对象,因此您需要将它们过滤掉。

    $ou = 'distinguishedName of my OU'
    Get-ADGroup -Filter * -SearchBase $ou -Properties 'Description' | ForEach-Object {
        $group   = $_  # just for convenience..
        $members = Get-ADGroupMember $_ | Where-Object { $_.objectClass -eq 'user' }
        foreach($member in $members) {
            $user = Get-ADUser $member -Properties EmailAddress
            [pscustomobject]@{
                SamAccountName = $user.SamAccountName
                Name           = $user.Name
                EmailAddress   = $user.EmailAddress
                GroupName      = $group.Name
                Description    = $group.Description
            }
        }
    } | Export-csv C:UsersMeDesktopMyFile.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多