Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何通过powershell检查exchange邮箱?_.net_Email_Powershell_Exchange Server - Fatal编程技术网

.net 如何通过powershell检查exchange邮箱?

.net 如何通过powershell检查exchange邮箱?,.net,email,powershell,exchange-server,.net,Email,Powershell,Exchange Server,如何使用powershell将收到的最后5封邮件的文本和标题返回到我的exchange电子邮件帐户?有没有一个简单的方法/库可以做到这一点 这是有关的。除了没有找到任何好的替代方案之外,我想我还可以编写自己的简单powershell客户端。您需要安装,并且需要在反射程序集加载部分检查DLL的路径 这将使您能够使用$inbox.FindItems(5)语句,并从中筛选您想要的结果 [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft

如何使用powershell将收到的最后5封邮件的文本和标题返回到我的exchange电子邮件帐户?有没有一个简单的方法/库可以做到这一点

这是有关的。除了没有找到任何好的替代方案之外,我想我还可以编写自己的简单powershell客户端。

您需要安装,并且需要在反射程序集加载部分检查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)

首先,我很抱歉,这个回复是在问题提出将近两年之后,但我也想使用Powershell检查电子邮件,并发现了这个问题。希望我的代码可以作为其他人从Powershell查看outlook的参考/起点。我计划自己增强它,使其更可用

我是Powershell的新手,所以我的脚本主要来自于各种文章、博客帖子和StackOverflow问答,当然,下面的脚本也不例外

根据Chris的回复,我在互联网上做了进一步的挖掘,拼凑了一些Powershell的片段,以便显示电子邮件中的一些关键信息

令人遗憾的是,它缺乏任何“适当”的风格,我相信任何Powershell大师都会对此感到畏缩。但这段代码的作用是

  • 演示如何使用EWS和Powershell阅读电子邮件
  • 解决George的最后一个问题:正文为空-FindItems方法不会返回完整的邮件项目,您必须再次往返以获取所需的额外信息
  • 通过使用当前凭据删除对您使用用户名/密码/域的要求
  • 删除“安装”EWS的要求,只需提取MSI并引用dll即可
使用

下载EWS,然后在某处提取,例如

msiexec/a C:\Path\To\Downloads\EwsManagedApi.msi/qb TARGETDIR=C:\Progs\EwsManagedApi

然后使用点源调用此脚本,例如

。C:\Path\To\Script\Outlook\u ReadInbox.ps1

它允许您在脚本执行后引用脚本中的对象/变量

代码中始终有有限的注释,最后还有一些链接,我在拼凑脚本时引用了这些链接

下面是我的alpha代码草稿,在前5封电子邮件中阅读,显示是否已读/未读,并在一行上显示电子邮件正文的前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

您需要在安装了Olook客户端的本地计算机或Exchange服务器(可能没有客户端)上的何处检查此问题?它必须是一个免费的解决方案吗?我想要一个替代方案,而不是仅仅为了从电子邮件中获取链接而打开outlook。所以我们假设outlook没有安装,是的,我希望它是免费的,因为我想把它放在PoshCode上。我一直在做一些挖掘工作,真的Pop3可能会工作得很好。谢谢Chris,反应很好,你知道为什么Body属性总是空的吗?它就像你警告的那样难看,但正是我需要的。好东西!:-)谢谢你的剧本!我修改了
$bod.Substring
行以处理小消息体或空消息体