Remove 'SMTP' from 'SMTP:email@domain.com' in PowerShell using Get-AzureADUser

懵懂的女人 提交于 2019-12-11 11:14:47

问题


I am using the below script to display aliases for users in O365 which I will eventually be exporting.

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { $_.ProxyAddresses -join ";"}}
    )

In the Aliases (ProxyAddresses) column, it displays all the aliases separated by a ; as expected but it also includes SMTP: in front of all of them.

Is there a way to remove the SMTP: from these values?

Current result: SMTP:email@domain.com;SMTP:email2@domain.com

Desired result: email@domain.com;email2@domain.com


回答1:


Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { $_.ProxyAddresses -replace '^smtp:' -join ';' }}
    )

This will replace smtp: at the beginning (^) of each proxyaddress, using PowerShell's ability to make operators automatically work on all members of an array, then join the results.




回答2:


If you also want to lose the ".onmicrosoft.com" addresses this should work

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { 
            $proxies = $_.ProxyAddresses -replace '^smtp:'
            $proxies = $proxies | Where-Object { $_ -notlike '*.onmicrosoft.com' }
            $proxies -join '; ' }
        }
    )

Mind you, the ProxyAddresses attribute can also hold other entries like SIP:upn@domain.com or X500: /o=company/ou=External (FYDIBOHF25SPDLT)/cn=Recipients/cn=userxxx

If you also would like to exclude those, use

Get-AzureADUser |
    Select-Object @(
        @{L = "Name"; E = { $_.DisplayName}}
        @{L = "Email"; E = { $_.UserPrincipalName}}
        @{L = "Aliases"; E = { 
            $proxies = $_.ProxyAddresses
            $proxies = $proxies | Where-Object { $_ -match '^smtp:' -and $_ -notlike '*.onmicrosoft.com' }
            $proxies -replace '^smtp:' -join '; ' }
        }
    )


来源:https://stackoverflow.com/questions/51503708/remove-smtp-from-smtpemaildomain-com-in-powershell-using-get-azureaduser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!