0

I have script that goes through my emails then returns the body of each of the emails that matches my query. The body looks something like this

Recipients:     [email protected] 
Attachment  Report.pdf 
Timestamp of Message:   18 January 2016 

Recipients:     [email protected] 
Attachment  Report2.pdf 
Timestamp of Message:   18 January 2016 

I'm trying to either get each line of the body in a custom-object or just extract the email addresses to a variable to do more work with. Any ideas.

5
  • Is it returning an array of single line strings, an array of multi-line strings (one per email body), or a single multi-line string with all of the bodies? Commented Jan 18, 2016 at 3:46
  • I'm actually not sure. How can I tell? Commented Jan 18, 2016 at 3:48
  • @mjolinor - I believe it is an array of multi-line strings Commented Jan 18, 2016 at 3:55
  • If you send it to a variable, what is the .count property of that vaiable? Commented Jan 18, 2016 at 3:55
  • @mjolinor - The count equals the number of emails found Commented Jan 18, 2016 at 3:56

1 Answer 1

3

One option is to use a multi-line regex to capture the data, and then create a custom object from the captures:

Edit: Updated with example using ForEach loop.

$regex = @'
(?ms)Recipients:\s+(.+?)\s* 
Attachment\s+(.+?)\s* 
Timestamp of Message:\s+(.+)
'@ 

$Result=
ForEach ($text in $variable)
 {
   if ($text -match  $regex)
     {
       [PSCustomObject]@{
        Recipients = $matches[1]
        Attachment = $matches[2]
        Timestamp  = $matches[3]
        }
     }
 }

Recipients                        Attachment  Timestamp      
----------                        ----------  ---------      
[email protected] Report2.pdf 18 January 2016
Sign up to request clarification or add additional context in comments.

5 Comments

In your example how do I add the variable from the output of my original query to the $text array
Since your variable is an array, you can run it through a ForEach loop.I'll update the answer with an example.
Had a copy/paste fail and left out the regex when I did the update. Fixed.
Man - I can't give you a +1 yet since I don't have enough of a rep yet. But that is awesome. Thanks
See my Answer below, but it's also a question sinc eI'm trying to add each output object to a variable with the properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.