本文介绍了在Powershell中使用Outlook PropertyAccessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Powershell ISE中做一个简单的任务。
我想要能够读取内容和头文件和电子邮件,并使用 write-host 在控制台中打印。



我想打印发件人的姓名,后跟他的地址。我想打印接收者和电子邮件的主题,其次是邮件的正文/内容。我虽然这样做会很简单,但是我有一个小小的挫折。



为了最小化返回值,我创建了一个只有一个e的文件夹 - 邮件里面和一个简短的信息,这样很容易打印出来并确认。






问题:



我可以打印出只有一个我想要的字段。当我运行脚本时,我只得到一个主题字段的值。所有其他(SenderName,SenderAddress,To)不给出值,但在研究之后,我确认我获取了这些属性的对象。






代码



 添加类型-assemblyMicrosoft.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
/ pre>

返回值






  • 这些字段在我的交换服务器中是否受到某种安全措施的保护?


  • 我的代码中有什么问题吗?


  • 为什么我无法访问这些值?我如何解决这个问题?




我对于Outlook的Powershell脚本来说很新,对我的问题的研究的公平份额在发布在这里之前,我根本找不到任何解释。
我已经阅读了一些名为PropertyAccessor的信息,但是从来没有找到这个用于powershell的信息,仅适用于vba。这可能是一个可能的解决方案?



谢谢。

解决方案

您尝试打印的字段应该默认为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:)

这篇关于在Powershell中使用Outlook PropertyAccessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:51