问题
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