简体   繁体   中英

Getting the proxyaddresses attribute of users

I have the following line of powershell code i was working on extracting user proxy addresses values. I need all smtp and/or SMTP values like below.

Get-ADUser -Filter * -Properties proxyaddresses | Select-Object Name, @{L = "ProxyAddresses"; E = { $_.ProxyAddresses -join ";"}} | Export-Csv -Path c:\temp\proxyaddresses.csv -NoTypeInformation

My output :

"Name","ProxyAddresses"
"John T","sip:john@contoso.com;smtp:john@contoso1.com;SMTP:john@contoso.com;smtp:john@contoso1.com;X400:C=US;A= ;P=ORG;O=Exchange;S=T;G=John;"

My Desired output :

"Name","ProxyAddresses"
"John T","smtp:john@contoso1.com;SMTP:john@contoso.com;smtp:john@contoso1.com"

Since the ProxyAddresses property will be interpreted as a collection of strings, you can use a Comparison Operator . The easiest options here are -like and -match .

Get-ADUser -Filter * -Properties proxyaddresses |
    Select-Object Name, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -match '^smtp:') -join ";"}}
# Or
Get-ADUser -Filter * -Properties proxyaddresses |
    Select-Object Name, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -like 'smtp:*') -join ";"}}

-like and -match are case-insensitive. You can append a c to the operator name for the case-sensitive versions. This could be useful when looking for the primary smtp address that begins with SMTP: . In that case, you can use:

Get-ADUser -Filter * -Properties proxyaddresses |
    Select-Object Name, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -cmatch '^SMTP:') -join ";"}}
# OR

Get-ADUser -Filter * -Properties proxyaddresses |
    Select-Object Name, @{L = "ProxyAddresses"; E = { ($_.ProxyAddresses -clike 'SMTP:*') -join ";"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM