3

I need to parse the body of emails to get the contents of a table out of each email. I would like to use PowerShell to do this. I have tried this code:

Get-ChildItem "C:\Users\mmartindale.NTSERVER\Desktop\delreg temp\msg" -Filter *.msg |
    ForEach-Object {
        $outlook = New-Object -comobject outlook.application
        $msg = $outlook.Session.OpenSharedItem($_.FullName)
        $msg | Select body | ft -AutoSize
    }

But all I get is an abbreviated output like this:

Body
----
This is approved for Deal Reg...

How do I output this as an object that I can loop through?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user2642759
  • 33
  • 1
  • 6

1 Answers1

2

You can use Select-Object's -ExpandProperty parameter to expand the property you want.

As in:

$msg | Select-Object -ExpandProperty Body
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • I'm really interested to see if you know of a way to close the $msg after it's been opened. The script will parse the e-mail body 1 time, but after that it complains of the message already being opened. – Brandon May 02 '17 at 18:41
  • @Brandon I'll do some testing. Why do you need to parse it multiple times? I've had this kind of issue with the client, and I think it's related to some security setting in Outlook (Trust Center). – sodawillow May 02 '17 at 19:41
  • @Brandon please open a new question with the code you use, I don't know what you mean by "parse it twice", and it shouldn't be dealt with in comments : ). – sodawillow May 02 '17 at 19:51