blog.icewolf.ch

Let's talk about IT!
posts - 1802, comments - 295, trackbacks - 0

My Links

Archives

Post Categories

icewolf

Remove AelitaEMW Messages in Public Folder with EWS Managed API

Hallo zusammen,

 Ich habe ein Powershell Script entwickelt um die Quest Migration Manager Messages nach der Migration im Public Folder zu entfernen. Das Script nutzt die EWS Managed API http://www.microsoft.com/en-us/download/details.aspx?id=35371

###############################################################################
# Remove Quest Migration Manager Hidden Messages in Public Folders
# Remove Messages with Subject "AelitaEMW_" in Associated Content Store
#
# Script Uses EWS Managed API
# http://www.microsoft.com/en-us/download/details.aspx?id=35371
#
# Version 1.0 / 27.10.2013
# Andres Bohren / www.icewolf.ch / blog.icewolf.ch / info@icewolf.ch
###############################################################################

 

###############################################################################
# PFRootListFolders
# List Public Folder Root Folders
###############################################################################

Function PFRootListFolders
{
    $View = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
           
    $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
    $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName)
    $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::ChildFolderCount)

    $View.PropertySet = $psPropertySet                                        
    $ExResult = $EWService.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot, $View)
           
    If ($ExResult -ne $Null)           
    {
        Write-Host ("#Get Folders")
        ForEach ($myFolder In $ExResult.Folders)
        {
            Write-Host ("FolderName: " + $myFolder.DisplayName + " FolderID: " + $myFolder.Id)
            PFListChildFolders($myFolder.ID)
        }
    }
}


###############################################################################
# PFListChildFolders
# List Child Folders of Publc Folder ID
###############################################################################

Function PFListChildFolders
{
    PARAM ([Microsoft.Exchange.WebServices.Data.FolderID]$pFolderID)

    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
           
    $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
    $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName)

    $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::ChildFolderCount)

    $View.PropertySet = $psPropertySet
                                       
    $ExResult = $EWService.FindFolders($pFolderID,$FolderView)
           
    If ($ExResult -ne $Null)           
    {
        #Write-Host ("#Get Child Folders")
        ForEach ($myFolder In $ExResult.Folders)
        {
            Write-Host ("FolderName: " + $myFolder.DisplayName + " FolderID: " + $myFolder.Id + " FolderClass: " + $myFolder.FolderClass)

            #Delete Aelita Message
            DeleteAelitaMessage($myFolder.ID)

            #Recursive Call This Function
            PFListChildFolders($myFolder.ID)
        }
    }
}

###############################################################################
# Function DeleteAelitaMessage
# Deletes AelitaMessage "AelitaEMW_" in AccociatedContentsTable
###############################################################################

Function DeleteAelitaMessage
{
  PARAM ([Microsoft.Exchange.WebServices.Data.FolderID]$pFolderID)

    Try {

        $ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) 
        $ItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated

        $ExResult = $EWService.FindItems($pFolderID,$ItemView)

        Foreach ($Item In $ExResult.Items)
        {
            If ($Item.Subject -like "AelitaEMW_*")
            {
                Write-Host ("DELETE THIS ITEM: subject=" + $Item.Subject + " LastModifiedTime=" + $Item.LastModifiedTime) -Foregroundcolor cyan
                $Item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
            } else {
                Write-Host ("Subject=" + $Item.Subject + " LastModifiedTime=" + $Item.LastModifiedTime) -Foregroundcolor green
            }

        }

    } catch [system.exception] {
                Write-Host ("Error in DeleteAelitaMessage: " + $_.Exception.ToString())       
    }

###############################################################################
# Main Programm
###############################################################################


#Global Variables
[string]$Mailbox = "ewsuser@domain.tld"
[string]$Username = "ewsuser"
[string]$Password = "PassWord"
[string]$Domain = "Domain"

# Load EWS Managed API DLL  and Connect to Exchange
[String] $dllPath = "D:\Scripts\EvLeaver\Microsoft.Exchange.WebServices.dll"
[Void] [Reflection.Assembly]::LoadFile($dllPath)
 
$EWService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$EWService.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials($Username,$Password,$Domain)
$EWService.AutodiscoverUrl($mbxName, {$True})

#Call Function
PFRootListFolders

Grüsse
Andres Bohren

Print | posted on Sunday, October 27, 2013 9:59 PM | Filed Under [ Exchange Powershell ]

Powered by:
Powered By Subtext Powered By ASP.NET