0

I have written a PowerShell script to delete all emails and inbox subfolders of a secondary mailbox open in my Outlook profile but to leave the rest of a users' mailbox intact. I want to know if there's a way of doing it without sending everything to the Deleted Items folder first.

I am working remotely over a VPN and this unnecessary step of moving files into my Delete Items is both slow and an annoying inconvenience. Is there any script equivalent to Shift+Delete?

Get-Member returns the following list of methods for the mail item object type. I've been using the Delete method but I can't find the object type reference on MSDN to have a closer look at its methods to see if Delete will take a parameter to permanently delete for example.

   TypeName: System.__ComObject#{00063034-0000-0000-c000-000000000046}

Name                              MemberType Definition                                            
----                              ---------- ----------                                            
AddBusinessCard                   Method     void AddBusinessCard (ContactItem)                    
ClearConversationIndex            Method     void ClearConversationIndex ()                        
ClearTaskFlag                     Method     void ClearTaskFlag ()                                 
Close                             Method     void Close (OlInspectorClose)                         
Copy                              Method     IDispatch Copy ()                                     
Delete                            Method     void Delete ()                                        
Display                           Method     void Display (Variant)                                
Forward                           Method     MailItem Forward ()                                   
MarkAsTask                        Method     void MarkAsTask (OlMarkInterval)                      
Move                              Method     IDispatch Move (MAPIFolder)                           
PrintOut                          Method     void PrintOut ()                                      
Reply                             Method     MailItem Reply ()                                     
ReplyAll                          Method     MailItem ReplyAll ()                                  
Save                              Method     void Save ()                                          
SaveAs                            Method     void SaveAs (string, Variant)                         
Send                              Method     void Send ()                                          
ShowCategoriesDialog              Method     void ShowCategoriesDialog ()                         

My script looks like this at the moment:

# Initialise variables
$TargetMailbox = "Mailbox - User"

# SCRIPT BODY
# ===========

# Connect to Outlook
$Outlook = New-Object -comobject Outlook.Application

# Select the mailbox by name
$Mailbox = $Outlook.Session.Folders | Where-Object { $_.Name -eq $TargetMailbox}

# Select Inbox by name
$Inbox = $Mailbox.Folders | Where-Object { $_.Name -eq "Inbox" }

# Delete subfolders
$Inbox.Folders | foreach {
    write-host "Deleting Inbox\$($_.Name) (Folder)"
    $_.Delete()
}

# Delete items
$Inbox.Items | foreach {
    write-host "Deleting Inbox\$($_.subject) (Item)"
    $_.Delete()
}
4

2 に答える 2

0

MailItem.Delete を呼び出してから、[削除済みアイテム] フォルダーでその削除済みアイテムを検索し (たとえば、カスタム プロパティを使用して)、MailItem.Delete を呼び出して再度削除することができます。

Redemption (Outlook オブジェクト モデルと一緒に使用できます) とそのRDOMail .Delete メソッドを使用することもできます。オプションの DeleteFlags パラメーター (dfSoftDelete、dfMoveToDeletedItems、dfHardDelete) を取ります。

于 2013-10-10T20:28:02.933 に答える
0

EWS ライブラリを使用して Exchange に接続できます。独自のメールボックスと対話するモデルは非常に簡単です。削除方法には、ハード削除、ソフト削除、およびゴミ箱へのモード オプションがあります。ローカルの Outlook プロファイルを完全にバイパスするため、標準の Server Sync を使用して同期を取り戻します。これが私が知っている最速の方法です。

このサンプルはカレンダーをクリアしますが、そのトリックは他のフォルダーでも非常に似ています。

_service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//Connect directly to the EWS uri, or use AutoDiscover
_service.Url = new Uri(ExchangeServerUri);
_service.Credentials = (NetworkCredential)CredentialCache.DefaultNetworkCredentials;

ItemView view = new ItemView(100, 0, OffsetBasePoint.Beginning);
view.PropertySet = new PropertySet(PropertySet.IdOnly);
view.PropertySet.Add(ItemSchema.Subject);
view.PropertySet.Add(AppointmentSchema.Start);
view.PropertySet.Add(AppointmentSchema.End);
view.PropertySet.Add(AppointmentSchema.AppointmentType);
view.PropertySet.Add(AppointmentSchema.TimeZone);
view.Traversal = ItemTraversal.Shallow;

Console.WriteLine("Fetching appointments for {0}", Mailbox);

FolderId folder = new FolderId(WellKnownFolderName.Calendar, new Mailbox(Mailbox));

foreach (var item in folder.Items)
     _service.DeleteItems(new[] { item.Id }, DeleteMode.HardDelete, SendCancellationsMode.SendToNone, AffectedTaskOccurrence.AllOccurrences);

その他の利点は、Outlook をインストールする必要がないこと、COM オートメーション オブジェクトに関するセキュリティの問題がないことなどです。

于 2013-10-10T18:30:27.790 に答える