且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Powershell中使用Outlook PropertyAccessor

更新时间:2023-02-13 11:23:41

您尝试打印的字段应该默认为afaik。



我假设它可能与打印时的方式相关,并在打印属性时将其合并。



如果您执行以下操作,应该向您显示发件人电子邮件地址的整个容器内容:

  foreach($ m ($ m.GetProperty)$($ m.SenderEmailAddress)
write-host主题:$($ m.Subject)
}

而不是使用for循环和使用写主机,我建议只选择您想要的属性。



示例:

  $ mails | select SenderEmailAddress,SenderName,Subject,ReceivedTime 

您还可以通过执行查看邮件对象上可用的属性以下:

  $ mails | get-member 

在这里的一个outlook 2007实例上测试了这个,这似乎在工作。



希望这有助于:)


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

  • 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.

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:)