
我厭倦了嘗試使用 Outlook 的 GUI 來建立/刪除/管理收件匣規則;這需要很長時間而且很糟糕。
據我所知,powershell 中有一些 cmdlet 允許您在伺服器端和用戶端執行此操作。
我無權訪問伺服器端,我只對使用這些規則管理我自己的收件匣感興趣。
下面的文章似乎提到了一些對此非常有用的 cmdlet,但我無法使用它們,因為我不知道從哪裡匯入庫:
https://www.codetwo.com/admins-blog/managing-outlook-rules-powershell/
如何先安裝 cmdlet?我在哪裡可以獲得它們,我需要用什麼將它們匯入到腳本中?
答案1
將其放在這裡是因為對於該評論部分來說太長了。
馴服您的郵箱是一個問題,您是在獨立客戶端(胖客戶端、雲端客戶端還是兩者- 請記住,您可以直接在Outlook 胖客戶端中添加您的Outlook.com 帳戶。)還是為您企業中的使用者說話?
您不使用 Exchange cmdlet 來控製本機 Outlook。若要使用 Exchange cmdlet,您必須在電腦上安裝 Exchange 命令列管理程式...
...或使用 PowerShell 隱式遠端處理將這些 cmdlet 代理到您的主機。隱式遠端處理也是您使用 O365 的 Exchange cmdlet 的方式。Microsoft 以及網路上和 Youtube 影片中的大量部落格對此都有詳細記錄/詳細資訊。
甚至為此工作預先建構了模組。
使用 PowerShell 連線至 Office 365 服務
具有說明程式功能的 PowerShell 腳本,用於連線到 Office 365 或 Exchange 本機。可以使用相同的憑證(非 MFA)輕鬆連接到不同的 Office 365 服務。不再需要記住連接到每個工作負載的各種方式。
Outlook Online 有一個 Rest API 可以與 Exchange、EWS 一起使用
Outlook用戶端
Outlook REST API 為任何 Office 365 或 Outlook.com 使用者提供了一種存取郵件、行事曆和聯絡人資料的簡單方法。在本影片中,我們將介紹 API V2.0 中引入的一些關鍵功能:Webhooks、照片、提醒和時區。
使用 PowerShell 對 Outlook 收件匣進行資料探勘
# Get-OutlookInBox function
Function Get-OutlookInBox
{
<#
.Synopsis
This function returns InBox items from default Outlook profile
.Description
This function returns InBox items from default Outlook profile. It
uses the Outlook interop assembly to use the olFolderInBox enumeration.
It creates a custom object consisting of Subject, ReceivedTime, Importance,
SenderName for each InBox item.
*** Important *** depending on the size of your InBox items this function
may take several minutes to gather your InBox items. If you anticipate
doing multiple analysis of the data, you should consider storing the
results into a variable, and using that.
.Example
Get-OutlookInbox |
where { $_.ReceivedTime -gt [datetime]”5/5/11″ -AND $_.ReceivedTime -lt `
[datetime]”5/10/11″ } | sort importance
Displays Subject, ReceivedTime, Importance, SenderName for all InBox items that
are in InBox between 5/5/11 and 5/10/11 and sorts by importance of the email.
.Example
Get-OutlookInbox | Group-Object -Property SenderName | sort-Object Count
Displays Count, SenderName and grouping information for all InBox items. The most
frequently used contacts appear at bottom of list.
.Example
$InBox = Get-OutlookInbox
Stores Outlook InBox items into the $InBox variable for further
“offline” processing.
.Example
($InBox | Measure-Object).count
Displays the number of messages in InBox Items
.Example
$InBox | where { $_.subject -match ‘2011 Scripting Games’ } |
sort ReceivedTime -Descending | select subject, ReceivedTime -last 5
Uses $InBox variable (previously created) and searches subject field
for the string ‘2011 Scripting Games’ it then sorts by the date InBox.
This sort is descending which puts the oldest messages at bottom of list.
The Select-Object cmdlet is then used to choose only the subject and ReceivedTime
properties and then only the last five messages are displayed. These last
five messages are the five oldest messages that meet the string.
.Notes
NAME: Get-OutlookInbox
AUTHOR: ed wilson, msft
LASTEDIT: 05/13/2011 08:36:42
KEYWORDS: Microsoft Outlook, Office
HSG: HSG-05-26-2011
.Link
Http://www.ScriptingGuys.com/blog
#Requires -Version 2.0
#>
Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items |
Select-Object -Property Subject, ReceivedTime, Importance, SenderName
} #end function Get-OutlookInbox
您必須是 O365 的管理員才能執行此操作。 O365 中的 Outlook 不是 Exchange,就像桌面上的 Outlook 不是 Exchange 一樣。您必須使用 Outlook COM(如上所述和後面所述)才能在 Outlook 用戶端上工作,這不適合線上。
答案2
據我所知,Outlook 沒有可用的標準 powershell 模組,但是您可以使用 Outlook API 來存取Microsoft.Office.Interop.Outlook
命名空間。建立新規則的簡短範例如下:
首先我們得到命名空間:
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
現在我們指定原始資料夾(收件匣),建立新規則並指定要複製到的資料夾(通知)
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$MyFolder1 =
$namespace.Folders.Item('[email protected]').Folders.Item('NOTIFICATIONS')
$rules = $namespace.DefaultStore.GetRules()
$rule = $rules.create("My rule1: Receiving Notification",
[Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive)
$rule_body = $rule.Conditions.Subject
$rule_body.Enabled = $true
$rule_body.Text = @('Completed Notification')
$action = $rule.Actions.CopyToFolder
$action.enabled = $true
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember(
"Folder",
[System.Reflection.BindingFlags]::SetProperty,
$null,
$action,
$MyFolder1)
$rules.Save()
您可以在此處找到有關在 powershell 中使用 Outlook 命名空間的更多範例和資訊: https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/powershell-managing-an-outlook-mailbox-with-powershell
此外,您還可以在此處找到有關 Outlook 物件模型的所有資訊: https://docs.microsoft.com/nl-nl/office/vba/api/overview/Outlook/object-model