我如何使用PowerShell将收到的最后5封邮件的文本和标题返回到我的交换电子邮件帐户?有没有简单的方法/库来做到这一点?
这与my question about not using outlook on superuser有关。除了没有找到任何好的替代方案,我想我也可以编写自己的简单PowerShell客户端。
答案 0 :(得分:22)
首先,道歉这个回复是问题发生后近两年,但我也想用Powershell检查电子邮件并发现这个问题。希望我的代码可以作为其他人寻求从Powershell挖掘Outlook的参考/起点。我打算自己加强这个以使它更有用。
我是Powershell的新手,所以我的剧本主要是Frankenstein-ed,来自各种文章,博客文章和StackOverflow Q& A当然,下面的剧本也不例外!
继Chris的回应之后,我在互联网上做了一些进一步的挖掘,并拼凑了一些Powershell的片段,让我可以通过电子邮件显示一些关键信息。
令人遗憾的是,缺乏任何“正确”的风格,我确信任何Powershell大师都会对此感到畏缩。但是这段代码的作用是
FindItems
方法不会返回完整的邮件项目,您必须再次往返才能获得所需的额外信息。使用......
下载EWS from here,然后在某处提取,例如
msiexec /a C:\Path\To\Downloads\EwsManagedApi.msi /qb TARGETDIR=C:\Progs\EwsManagedApi
然后使用dot-source调用此脚本,例如
. C:\Path\To\Script\Outlook_ReadInbox.ps1
允许您在脚本执行后从脚本中引用对象/变量。
代码在整个过程中都有有限的注释,最后还有一些链接,我在将脚本拼凑在一起时引用了这些链接。
这是我在前5封电子邮件中阅读的代码的alpha草案,显示是否已阅读/未阅读,并在删除了空格的一行中显示电子邮件正文的前100个字符。
# work with exchange server to retrieve messages
# see this SO answer: http://stackoverflow.com/a/4866894
# call this script using dot-source (see http://technet.microsoft.com/en-us/library/ee176949.aspx)
# to allow continued use of the objects, specifically, reading our inbox
# e.g...
# . C:\Path\To\Script\Outlook_ReadInbox.ps1
# replace with your email address
$email = "your.name@yourdomain.com"
# only need to populate these if you're impersonating...
$username = "YOUR_USER_NAME"
$password = "YOUR_LAN_PASSWORD"
$domain = "YOUR_DOMAIN"
# to allow us to write multi-coloured lines
# see http://stackoverflow.com/a/2688572
# usage: Write-Color -Text Red,White,Blue -Color Red,White,Blue
# usage: Write-Color Red,White,Blue Red,White,Blue
function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
for ($i = 0; $i -lt $Text.Length; $i++) {
Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
}
Write-Host
}
# load the assembly
[void] [Reflection.Assembly]::LoadFile("C:\Progs\EwsManagedApi\Microsoft.Exchange.WebServices.dll")
# set ref to exchange, first references 2007, 2nd is 2010 (default)
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
#$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
# use first option if you want to impersonate, otherwise, grab your own credentials
#$s.Credentials = New-Object Net.NetworkCredential($username, $password, $domain)
#$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$s.UseDefaultCredentials = $true
# discover the url from your email address
$s.AutodiscoverUrl($email)
# get a handle to the inbox
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
#create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
$items = $inbox.FindItems(5)
#set colours for Write-Color output
$colorsread = "Yellow","White"
$colorsunread = "Red","White"
# output unread count
Write-Color -Text "Unread count: ",$inbox.UnreadCount -Color $colorsread
foreach ($item in $items.Items)
{
# load the property set to allow us to get to the body
$item.load($psPropertySet)
# colour our output
If ($item.IsRead) { $colors = $colorsread } Else { $colors = $colorsunread }
#format our body
#replace any whitespace with a single space then get the 1st 100 chars
$bod = $item.Body.Text -replace '\s+', ' '
$bodCutOff = (100,$bod.Length | Measure-Object -Minimum).Minimum
$bod = $bod.Substring(0,$bodCutOff)
$bod = "$bod..."
# output the results - first of all the From, Subject, References and Message ID
write-host "====================================================================" -foregroundcolor White
Write-Color "From: ",$($item.From.Name) $colors
Write-Color "Subject: ",$($item.Subject) $colors
Write-Color "Body: ",$($bod) $colors
write-host "====================================================================" -foregroundcolor White
""
}
# display the newest 5 items
#$inbox.FindItems(5)
# display the unread items from the newest 5
#$inbox.FindItems(5) | ?{$_.IsRead -eq $False} | Select Subject, Sender, DateTimeSent | Format-Table -auto
# returns the number of unread items
# $inbox.UnreadCount
#see these URLs for more info
# EWS
# folder members: https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.folder_members%28v=exchg.80%29.aspx
# exporting headers: http://www.stevieg.org/tag/how-to/
# read emails with EWS: https://social.technet.microsoft.com/Forums/en-US/3fbf8348-2945-43aa-a0bc-f3b1d34da27c/read-emails-with-ews?forum=exchangesvrdevelopment
# Powershell
# multi-color lines: http://stackoverflow.com/a/2688572
# download the Exchange Web Services Managed API 1.2.1 from
# http://www.microsoft.com/en-us/download/details.aspx?id=30141
# extract somewhere, e.g. ...
# msiexec /a C:\Users\YourUsername\Downloads\EwsManagedApi.msi /qb TARGETDIR=C:\Progs\EwsManagedApi
答案 1 :(得分:15)
您需要安装EWS API,并且需要在Reflection Assembly加载部分中检查DLL的路径。
这应该让你能够使用$ inbox.FindItems(5)语句并过滤掉你想要的结果。
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll")
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = New-Object Net.NetworkCredential('user', 'pass', 'domain')
$s.AutodiscoverUrl("email@address.com")
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$inbox.FindItems(5)