0

I am creating a reporting tool for O365 using PS. My current issue is when grabbing any existing proxy adresses for a user I get the following data.

smtp:1stline@hatchellwood.onmicrosoft.com smtp:1stline@hatchell.com SMTP:1stline@hatchellwood.com SIP:1stline@hatchellwood.com SPO:SPO_024de2bf-d843-48e0-938d-fb6cfa8dcdbc@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7

I need to take the data only preceded by 'smpt' and remove the rest. I can't use the substring function due to the length of the substrings being random. Does anyone have a suggestion on how to approach this?

Much thanks.

2 Answers2

2
$Text = 'smtp:1stline@hatchellwood.onmicrosoft.com smtp:1stline@hatchell.com SMTP:1stline@hatchellwood.com SIP:1stline@hatchellwood.com SPO:SPO_024de2bf-d843-48e0-938d-fb6cfa8dcdbc@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7'
($Text |Select-String '(?<=smtp:)\S+(?!=\S)' -AllMatches).Matches.Value
1stline@hatchellwood.onmicrosoft.com
1stline@hatchell.com
1stline@hatchellwood.com
iRon
  • 20,463
  • 10
  • 53
  • 79
  • This doesn't return anything - I swapped the $a variable with the object variable holding the data above – Alistair Trout Aug 31 '22 at 12:03
  • Maybe my question was wrong then, if I try run this command I still get a no return. My data comes from an object I've grabbed from the Exchange module on a O365 tenant. EmailAddresses -------------- {smtp:A.Gibson@hatchellwood.onmicrosoft.com, SMTP:A.Gibson@hatchellwood.com, SIP:A.Gibson@hatchellwood.com, SPO:SPO_eb33fd9c-d0ae-417b-afb0-99a64d9f59c7@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7} I need it to be case sensitive as well as the STMP and stmp are different types. :/ – Alistair Trout Aug 31 '22 at 12:14
  • Note that I change the regular expression a bit at the last entry might not be closed with a space (`\s`) but anything that is **not** something else then a non-space (`\S`) which includes *nothing*. – iRon Aug 31 '22 at 12:15
  • No that part is great thank you I think it may be the actual data I'm passing into it. I use the Get-Mail cmdlet and take the EmailAddresses property which I'm assuming is technically not a string? Sorry very new to powershell. – Alistair Trout Aug 31 '22 at 12:17
  • $Mailboxes = Get-Mailbox $mailHold = $Mailboxes | Where-Object{$_.PrimarySmtpAddress -like $UserAboutInfo.UserPrincipalName} $UserMail = $mailHold | Select-Object EmailAddresses So that returns the value I put earlier with the various smtp,SMTP,SPO etc – Alistair Trout Aug 31 '22 at 12:19
  • 1
    See [Select the values of one property on all objects of an array in PowerShell](https://stackoverflow.com/q/5176815/1701026) – iRon Aug 31 '22 at 12:22
  • Ok so I now have a System.Array rather than an object thank you. – Alistair Trout Aug 31 '22 at 12:26
  • I do not have access to this `Get-Mailbox` cmdlet, but the property name **EmailAddress*es*** suggests that it is multi-value. Meaning that you might just do something like: `$mailHold | Select-Object -ExpandProperty EmailAddresses -like 'smpt:*'` – iRon Aug 31 '22 at 12:32
1

Not that this is any better than iRon's answer, but for those who prefer to split strings:

$ProxyAddresses = 'smtp:1stline@hatchellwood.onmicrosoft.com smtp:1stline@hatchell.com SMTP:1stline@hatchellwood.com SIP:1stline@hatchellwood.com SPO:SPO_024de2bf-d843-48e0-938d-fb6cfa8dcdbc@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7'
@(-split $ProxyAddresses) -match '^smtp:' -replace '^smtp:'
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27