powershellexchange-serverdisplayname-attribute

Need SMTP Address for Accepted Senders, ManagedBy and ModeratedBy in Distribution List


So I'm working in PowerShell to pull some data down from my exchange server.

I'm looking to get the following fields from the distribution list.

Display Name, SAM Account Name, Primary SMTP Address, Accepted Senders, Moderation Enabled, ModeratedBy, Internal Senders Only, and Managed By.

I'm using the below script to do this.

$props = @(
    "DisplayName"
    "SamAccountName"
    "PrimarySmtpAddress"
    @{Name="Accepted Senders";Expression= {(([string]($_.AcceptMessagesOnlyFromSendersOrMembers | foreach {$_.tostring().split("/")[-1]+';'})).TrimEnd(";") | foreach {$_.split(", ")[2,3,0]})}}
    "ModerationEnabled"
    @{Name="ModeratedBy";Expression= {([string]($_.ModeratedBy | foreach {$_.tostring().split("/")[-1]+';'})).TrimEnd(";")}}
    @{Name="Internal Senders Only";E={$_.RequireSenderAuthenticationEnabled}}
    @{Name="ManagedBy";E= {(([string]($_.ManagedBy | foreach {$_.tostring().split("/")[-1]+';'})).TrimEnd(";").split(", ")[2,3,0])}}
)

Get-DistributionGroup -ResultSize Unlimited | select $props | export-Csv x:\xxxxx\test6.csv -NoTypeInformation

Which works basically perfectly, except that is lists the display name of the Accepted Senders, ManagedBy and ModeratedBy instead of the smtp address.

To make it even more interesting, the email smtp format is first.last@company.com while the display names are Last, First often with additional words such as inactive and mixed in.

I have been able to format the data for managedby and accepted Senders so that the names show as first last as long as there is only one name and it doesn't have additional words in the Display Name, but I can't get it to insert a period so that I can pipe the output to a get-aduser request for the SMTP.

Anyway, let me know if you can help.

Ryan


Solution

  • If you want to interact with AD, you can do the following with your calculated property:

    @{
        n='Accepted Senders'
        e={($_.acceptmessagesonlyfromsendersormembers | Foreach-Object {
        (Get-AdUser -Filter "DisplayName -eq '$_'" -Property ProxyAddresses |
            Select -Expand ProxyAddresses | Where-Object {$_ -cmatch '^SMTP:'}) -replace '^SMTP:'}) -join ';'}
    }
    

    I don't know if your code $_.tostring().split("/")[-1] is causing you problems or not. However, if you need that functionality, you can change to the following:

    @{
        n='Accepted Senders'
        e={($_.acceptmessagesonlyfromsendersormembers | Foreach-Object {
        (Get-AdUser -Filter "DisplayName -eq '$($_.Split('/')[-1])'" -Property ProxyAddresses |
            Select -Expand ProxyAddresses | Where-Object {$_ -cmatch '^SMTP:'}) -replace '^SMTP:'}) -join ';'}
    }