Search code examples
exchangewebservices

How do I get EWS conflictresolutionmode to work


I will need to go through many mailboxes and remove 'copy:' from the subject line of lots of meetings. At the moment the code works fine except that after the code performs the conflictresoltuionmode, alwaysoverwrite and I can see the successful result it does not save the update.

I have tried using the save function but its not working

#Variables and constants
$startdate = Get-Date
$enddate = $startdate.AddDays(365)
#$string = 'check'


#create a remote session to exchange
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeserver.com/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking:$disablenamechecking

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
[Void] [Reflection.Assembly]::LoadFile($dllPath) 

$MailboxName = "firstname.lastname@company.com"
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchVer)
$exchservice.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
$exchService.UseDefaultCredentials = $true
$exchService.AutodiscoverUrl($MailboxName)
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar)
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,1000)
$FindItems = $calendar.FindAppointments($CalendarView)
if($FindItems.Items.Count -gt 0)
    {
      foreach($Item in $finditems){
          if ($item.Subject -like "check*") {
                $item.subject.substring(6)
                **$item.update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverWrite)
                # the overwrite is successful but it's not saving**
                #$item.Subject
                }
      #$ItemColl.Add($Item)
            }
    }

 $item.Subject

Solution

  • Your code doesn't appear to actually make any change eg

    $item.subject.substring(6)
    

    Would simply print the first 6 character of the Subject if you wanted to change the subject you would need to have

    $item.subject = "blah blah etc"

    You don't have to call save as its only valid if the object is new all that is need is Update but it will only do anything if there is a property that has been changed which there isn't in the above example.