如何通過powershell查看exchange郵箱?
我將如何使用 powershell 將收到的最後 5 條消息的文本和標題返回到我的交換電子郵件帳戶?有沒有簡單的方法/庫來做到這一點?
這與我關於不在 superuser 上使用 outlook 的問題有關。除了沒有找到任何好的替代方案之外,我認為我還不如編寫自己的簡單 powershell 客戶端。
您需要安裝EWS API,並且需要檢查反射程序集載入部分中 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)
2022 年 1 月 18 日更新:EWS 正在/已被淘汰。請參閱以下連結:
首先,很抱歉這個回復是在這個問題之後將近兩年,但我也想使用 Powershell 檢查電子郵件並找到這個問題。希望我的程式碼可以作為其他人從 Powershell 探勘前景的參考/起點。我計劃自己增強它以使其更有用。
我對 Powershell 很陌生,所以我的腳本主要來自各種文章、部落格文章和 StackOverflow Q&A 的科學怪人,當然,下面的腳本也不例外!
在 Chris 的回復之後,我在網際網路上做了一些進一步的探勘,並拼湊了一些 Powershell 片段,以允許我顯示來自電子郵件的一些關鍵資訊。
遺憾的是,它缺乏任何“適當”的風格,我相信任何 Powershell 大師都會對此感到畏縮。但是這段程式碼所做的是
- 展示如何使用 EWS 和 Powershell 閱讀電子郵件
- 解決喬治的最後一個問題:正文為空白 - 該
FindItems方法不會返回完整的郵件項目,您必須進行另一次往返以獲得所需的額外資訊。- 通過使用您目前的憑據刪除您使用使用者名/密碼/域的要求
- 刪除“安裝”EWS 的要求,只需提取 MSI 並引用 dll
要使用…
從此處下載 EWS ,然後在某處提取,例如
msiexec /a C:\Path\To\Downloads\EwsManagedApi.msi /qb TARGETDIR=C:\Progs\EwsManagedApi然後使用點源呼叫此腳本,例如
. 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