Home > Mobile >  Extracting specific data from a string based on value using PS
Extracting specific data from a string based on value using PS

Time:09-01

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:[email protected] smtp:[email protected] SMTP:[email protected] SIP:[email protected] 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.

CodePudding user response:

$Text = 'smtp:[email protected] smtp:[email protected] SMTP:[email protected] SIP:[email protected] SPO:SPO_024de2bf-d843-48e0-938d-fb6cfa8dcdbc@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7'
($Text |Select-String '(?<=smtp:)\S (?!=\S)' -AllMatches).Matches.Value
[email protected]
[email protected]
[email protected]

CodePudding user response:

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

$ProxyAddresses = 'smtp:[email protected] smtp:[email protected] SMTP:[email protected] SIP:[email protected] SPO:SPO_024de2bf-d843-48e0-938d-fb6cfa8dcdbc@SPO_1fef7b7b-eef2-4533-b45e-f7f842c9f6a7'
@(-split $ProxyAddresses) -match '^smtp:' -replace '^smtp:'
  • Related