使用Get-AzureADUser在PowerShell中从“ SMTP:email@domain.com”中删除“ SMTP”

时间:2018-07-24 16:40:48

标签: powershell email scripting smtp azure-active-directory

我正在使用以下脚本为O365中的用户显示别名,这些别名最终将要导出。

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

在别名(ProxyAddresses)列中,按预期显示用;分隔的所有别名,但在所有别名之前还包含SMTP:

是否可以从这些值中删除SMTP:

当前结果SMTP:email@domain.com;SMTP:email2@domain.com

所需结果email@domain.com;email2@domain.com

2 个答案:

答案 0 :(得分:2)

re := regexp.MustCompile(`\d{3}`)
search := "Vlan xyz site number 777 "
match := re.FindString(search)
fmt.Printf("OK: %q\n", match)
// OK: "777"

这将使用PowerShell的功能使运算符自动处理数组的所有成员,然后加入结果,从而在每个代理地址的开头(Get-AzureADUser | Select-Object @( @{L = "Name"; E = { $_.DisplayName}} @{L = "Email"; E = { $_.UserPrincipalName}} @{L = "Aliases"; E = { $_.ProxyAddresses -replace '^smtp:' -join ';' }} ) )处替换smtp:

答案 1 :(得分:0)

如果您还想丢失“ .onmicrosoft.com”地址,这应该可以解决

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 '; ' }
        }
    )

请记住,ProxyAddresses属性还可以保存其他条目,例如SIP:upn@domain.comX500: /o=company/ou=External (FYDIBOHF25SPDLT)/cn=Recipients/cn=userxxx

如果您还想排除这些,请使用

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 '; ' }
        }
    )