{"id":2246,"date":"2025-12-09T10:24:44","date_gmt":"2025-12-09T10:24:44","guid":{"rendered":"https:\/\/stevenwhiting.com\/blog\/?p=2246"},"modified":"2025-12-09T10:24:44","modified_gmt":"2025-12-09T10:24:44","slug":"smtp-alias","status":"publish","type":"post","link":"https:\/\/stevenwhiting.com\/blog\/?p=2246","title":{"rendered":"SMTP\/Alias"},"content":{"rendered":"\n<p>For AD SMTP and Alias&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;#\n.SYNOPSIS\n    Export SMTP addresses (primary + secondary) from AD users.\n\n.DESCRIPTION\n    Uses Get-ADUser to collect the 'proxyAddresses' and 'mail' attributes.\n    - Primary SMTP is the address with the \"SMTP:\" prefix (uppercase).\n    - Other SMTP addresses are those with the \"smtp:\" prefix (lowercase).\n    - If proxyAddresses is empty, the script will attempt to use the 'mail' attribute as primary.\n\n.PARAMETER OutputCsv\n    File path for exported CSV. Default: .\\AD-SMTP-Addresses.csv\n\n.PARAMETER SearchBase\n    Optional AD container (distinguishedName) to restrict the search.\n\n.PARAMETER IncludeDisabled\n    If specified, include disabled accounts as well. By default disabled accounts are excluded.\n\n.EXAMPLE\n    .\\Export-AD-SMTP.ps1 -OutputCsv C:\\temp\\smtp-addresses.csv\n\n#>\n\nparam(\n    &#91;string]$OutputCsv = \".\\AD-SMTP-Addresses.csv\",\n    &#91;string]$SearchBase,\n    &#91;switch]$IncludeDisabled\n)\n\n# Ensure ActiveDirectory module is available\nif (-not (Get-Module -ListAvailable -Name ActiveDirectory)) {\n    Write-Error \"The ActiveDirectory module is not installed or available. Install RSAT\/Active Directory module and run again.\"\n    exit 1\n}\n\nImport-Module ActiveDirectory -ErrorAction Stop\n\n# Build filter\n$filter = if ($IncludeDisabled) { { } } else { { Enabled -eq $true } }\n\n# Properties we need\n$properties = @(\"proxyAddresses\",\"mail\",\"distinguishedName\",\"samAccountName\",\"displayName\",\"objectClass\")\n\ntry {\n    if ($SearchBase) {\n        $users = Get-ADUser -Filter * -SearchBase $SearchBase -Properties $properties\n    } else {\n        $users = Get-ADUser -Filter * -Properties $properties\n    }\n}\ncatch {\n    Write-Error \"Failed to query Active Directory: $_\"\n    exit 1\n}\n\n$result = foreach ($u in $users) {\n    # Some objects may not have proxyAddresses (or not be users) - handle safely\n    $proxy = @()\n    if ($u.proxyAddresses) {\n        $proxy = $u.proxyAddresses\n    }\n\n    # Normalize and split SMTP addresses\n    $primary = $null\n    $others  = @()\n\n    if ($proxy.Count -gt 0) {\n        # find exact uppercase SMTP: for primary\n        $primaryEntry = $proxy | Where-Object { $_ -like \"SMTP:*\" } | Select-Object -First 1\n        if ($primaryEntry) {\n            $primary = $primaryEntry -replace '^&#91;sS]&#91;mM]&#91;tT]&#91;pP]:',''  # remove prefix (case-insensitive)\n        } else {\n            # no uppercase SMTP found -> try any smtp: entry as fallback\n            $fallback = $proxy | Where-Object { $_ -match '^(smtp|SMTP):' } | Select-Object -First 1\n            if ($fallback) { $primary = $fallback -replace '^&#91;sS]&#91;mM]&#91;tT]&#91;pP]:','' }\n        }\n\n        $others = $proxy |\n            Where-Object { $_ -match '^(smtp|SMTP):' } |\n            Where-Object { ($_ -replace '^&#91;sS]&#91;mM]&#91;tT]&#91;pP]:','') -ne $primary } |\n            ForEach-Object { $_ -replace '^&#91;sS]&#91;mM]&#91;tT]&#91;pP]:','' }\n    }\n\n    # If no proxyAddresses, fall back to mail attribute as primary (if present)\n    if (-not $primary -and $u.mail) {\n        $primary = $u.mail\n    }\n\n    # Build object for export\n    &#91;PSCustomObject]@{\n        DistinguishedName = $u.DistinguishedName\n        SamAccountName    = $u.SamAccountName\n        DisplayName       = $u.DisplayName\n        ObjectClass       = $u.ObjectClass\n        PrimarySMTP       = $primary\n        OtherSMTPs        = if ($others.Count -gt 0) { $others -join \";\" } else { $null }\n    }\n}\n\n# Export to CSV\ntry {\n    $result | Export-Csv -Path $OutputCsv -NoTypeInformation -Encoding UTF8\n    Write-Output \"Export completed: $OutputCsv ('$($result.Count) records)'\"\n}\ncatch {\n    Write-Error \"Failed to export CSV: $($_)\"\n    exit 1\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For AD SMTP and Alias&#8217;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[346],"tags":[333],"class_list":["post-2246","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2246"}],"version-history":[{"count":1,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2246\/revisions"}],"predecessor-version":[{"id":2247,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2246\/revisions\/2247"}],"wp:attachment":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}