0

I'm trying to do a simple task in Powershell ISE. I want to be able to read the content and headers of and e-mail and print it in the console using write-host.

I want to print the sender's name, followed by his address. I want to print out the receiver and the e-mail's subject, followed by the body/Content of the mail. I though this would be an easy task, but I've got a minor setback.

For sake of minimizing the return values I've created a folder with only one e-mail inside and a short message so it would be easy to print out and confirm.


The Problem:

I can print out only one of my desired fields. When I run the script I only get a value for the "Subject" field. All others (SenderName, SenderAddress, To) don't give a value but after research I have confirmed that I do get objects for these properties.


The Code

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")

$box = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderJunk)
$mails = $box.items
    
$mails|select  SenderName, SenderEmailAddress, To, Subject

Return Values

enter image description here

  • Could these fields be protected by some sort of security measure in my exchange server?

  • Am I doing anything wrong in my code?

  • Why can't I access these values? How can I fix this?

I'm quite new to Powershell scripts for Outlook, but I've done my fair share of research on my problem before posting it here, I simply can't find any explanation. I've read some info about something called PropertyAccessor but never found this for powershell, only for vba. Could this be a possible solution?

Thank you.

1
  • @Matt When I change it to for example: $m.GetProperty($m.SenderName) I get error : Method invocation failed because [System.__ComObject] doesn't contain a method named 'GetProperty'. Commented Feb 22, 2016 at 13:05

1 Answer 1

1

The fields you're trying to print should be available by default afaik.

I'm assuming it might be something related to the way you're printing and concating your properties when printing them.

If you do the following it should show you the entire container content of sender email address:

foreach($m in $mails){
    write-host "From:    $($m.GetProperty) $($m.SenderEmailAddress)"
    write-host "Subject:   $($m.Subject)"
}  

Instead of using the for loop and using write-host I'd recommend of just selecting the properties you want.

Example:

$mails|select SenderEmailAddress, SenderName, Subject, ReceivedTime

You can also check what properties are available on your mail object by doing the following:

$mails|get-member

Tested this on an outlook 2007 instance here which seems to be working.

Hope this helps:)

Sign up to request clarification or add additional context in comments.

7 Comments

I have edited question to your added info. Result of change also posted, please check it out. As you see, no change. I have checked various times with the | gm command and tried to access property values but none of the ones I need give my any more detail. Only pass me that it is a System.__ComObject but I can't read out it's details.
What version of outlook are you running? There's a security update; which if installed, will "require" a user prompt (though beeing hidden) when accessing the com, resulting in the address to fail / or be empty. This also goes if you try to use the "reply" on the message itself and get the receipients from this object. So if this is the case you'd need a workaround. You could take a look at the Redemption library which solves this : dimastr.com/redemption/home.htm
If you're on a newer exchange server, I'd highly recommend on using something else than the old-school com objects though! Unless you're relying on COM
Haven't looked in to many alternatives yet. Got some ideas to use Powershell com objects so I'm just looking in to using them. Currently using Outlook 2010. I'll try the redemption library to work around any possible Security Measures and let you know if any of it helped. Can you possibly provide me with some documentation of this Security patch so I can read the why and how ? Thanks! ;)
The patch is fairly old, but still applies to new versions. This site has a fairly good summary : slipstick.com/outlook/outlook-email-security-update . I think they wrote something about the patch and the redemption provider as well.
|

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.