Friday, September 28, 2012

Finding and removing Duplicate Items with EWS and Powershell (MEC Sample)

This is the first of a number of samples I'll be posting (in no specific order) from the talk I gave at MEC thanks to all those that did brave my talk and all the other people I got to talk with about EWS over the conference. For me MEC the missing conference was really unmissable and would recommend if you didn't catch it this time look out for it in the future also check out the new community site iammec.

So let's look at the method this script uses to detect duplicates, firstly there are a number of way that dups can be created and the method this script uses won't work in every situation so it's important to understand how it works. The primary Extended Property this script uses to detect duplicate Items is the pidTagSearchKey (or PR_Search_Key) http://msdn.microsoft.com/en-us/library/office/cc815908.aspx . If you read this link or your a fan of using a MAPI editor like me you would know that this property is used on both the Item and on the recipients in the recipients collection. The reason this property is useful for finding duplicates is that its "Unique in the Entire World" and doesn't change during a copy operation. So for cases where users have incorrectly copied Items (folder to folder, pst to mailbox, mailbox to archive) or they have been copied/Imported by some other method (and there are many) this should work. Where it won't work is when you just have a rouge app or piece of code that creates Items because each newly created Item in this case would have a unique SearchKey in that case you'll need to pick another property you want to use. As a secondary property this script uses the ReceivedTime which also shouldn't change when you copy an Item. The other thing to be careful of with this method is if the duplicates have been around for a while and you have changes being made to one of the duplicates and not the other. This script won't delete any duplicates it finds within a folder instead it will create a folder in the DeletedItems folder called detected duplicates with the datetime the script ran and it will then copy those detected duplicates to this folder. So if all goes wrong you have the ability to copy the items back otherwise they will get deleted when the user empties their deletedItems folder (which maybe never).

This script has one other new piece of code that I came up with for MEC which is the folder selector code snipit so unlike a lot of the scripts I've shown before where you need to set the Mailbox folder you want to work with or use some code to find that folder if its not a well-known folder when you run this script it will first do a findfolder operation to grab all the folders in a mailbox. It will then build a TreeView of all the Mailbox Folders and then present a GUI that will allow you to select what mailbox folder you want to work with.eg

 

 Then the rest of the code will run based on the doubleclick event on the particular leaf you select and doubleclick. The EWS FolderId is stored and retrieved from the tag property of each leaf of the treeview. I've latched this script meaning you need to confirm every duplicate it finds but I've also now added an all option which means if you don't want to have to confirm every Item you can just say yes once.

To run this script just pass in the primary SMTPAddress of the mailbox you want to run it against and it will prompt for the credentials. eg  .\finddups.ps1 mec@msgdevelop.onmicrosoft.com

I've put a download copy of the script here the code itself look like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"    
  7. [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   
  8. [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")   
  9.     
  10. ## Set Exchange Version    
  11. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  12.     
  13. ## Create Exchange Service Object    
  14. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  15.     
  16. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  17.     
  18. #Credentials Option 1 using UPN for the windows Account    
  19. $psCred = Get-Credential    
  20. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  21. $service.Credentials = $creds        
  22. $service.EnableScpLookup = $false  
  23.     
  24. #Credentials Option 2    
  25. #service.UseDefaultCredentials = $true    
  26.     
  27. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  28.     
  29. ## Code From http://poshcode.org/624  
  30. ## Create a compilation environment  
  31. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  32. $Compiler=$Provider.CreateCompiler()  
  33. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  34. $Params.GenerateExecutable=$False  
  35. $Params.GenerateInMemory=$True  
  36. $Params.IncludeDebugInformation=$False  
  37. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  38.   
  39. $TASource=@' 
  40.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  41.     public class TrustAll : System.Net.ICertificatePolicy { 
  42.       public TrustAll() {  
  43.       } 
  44.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  45.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  46.         System.Net.WebRequest req, int problem) { 
  47.         return true; 
  48.       } 
  49.     } 
  50.   } 
  51. '@   
  52. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  53. $TAAssembly=$TAResults.CompiledAssembly  
  54.   
  55. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  56. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  57. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  58.   
  59. ## end code from http://poshcode.org/624  
  60.     
  61. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  62.     
  63. #CAS URL Option 1 Autodiscover    
  64. $service.AutodiscoverUrl($MailboxName,{$true})    
  65. "Using CAS Server : " + $Service.url     
  66.      
  67. #CAS URL Option 2 Hardcoded    
  68.     
  69. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  70. #$service.Url = $uri      
  71.     
  72. ## Optional section for Exchange Impersonation    
  73.     
  74. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  75. #Define Extended properties    
  76. $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);    
  77. $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)    
  78. # Bind to the Contacts Folder  
  79.   
  80. $rfRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderidcnt)  
  81.   
  82. #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling    
  83. $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)    
  84. #Deep Transval will ensure all folders in the search path are returned    
  85. $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;    
  86. $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  87. $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);    
  88. #Add Properties to the  Property Set    
  89. $psPropertySet.Add($PR_Folder_Path);    
  90. $fvFolderView.PropertySet = $psPropertySet;    
  91. #The Search filter will exclude any Search Folders    
  92. $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")    
  93. $fiResult = $null    
  94. #  
  95.   
  96. $Treeinfo = @{}  
  97. $TNRoot = new-object System.Windows.Forms.TreeNode("Root")  
  98. $TNRoot.Name = "Mailbox"  
  99. $TNRoot.Text = "Mailbox - " + $MailboxName  
  100. #The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox    
  101. do {    
  102.     $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)    
  103.     foreach($ffFolder in $fiResult.Folders){     
  104.         #Process folder here  
  105.         $TNChild = new-object System.Windows.Forms.TreeNode($ffFolder.DisplayName.ToString())  
  106.         $TNChild.Name = $ffFolder.DisplayName.ToString()  
  107.         $TNChild.Text = $ffFolder.DisplayName.ToString()  
  108.         $TNChild.tag = $ffFolder.Id.UniqueId.ToString()  
  109.         if ($ffFolder.ParentFolderId.UniqueId -eq $rfRootFolder.Id.UniqueId ){  
  110.             $ffFolder.DisplayName  
  111.             [void]$TNRoot.Nodes.Add($TNChild)   
  112.             $Treeinfo.Add($ffFolder.Id.UniqueId.ToString(),$TNChild)  
  113.         }  
  114.         else{  
  115.             $pfFolder = $Treeinfo[$ffFolder.ParentFolderId.UniqueId.ToString()]  
  116.             [void]$pfFolder.Nodes.Add($TNChild)  
  117.             if ($Treeinfo.ContainsKey($ffFolder.Id.UniqueId.ToString()) -eq $false){  
  118.                 $Treeinfo.Add($ffFolder.Id.UniqueId.ToString(),$TNChild)  
  119.             }  
  120.         }  
  121.     }   
  122.     $fvFolderView.Offset += $fiResult.Folders.Count  
  123. }while($fiResult.MoreAvailable -eq $true)    
  124. $Script:clickedFolder = $null  
  125. $objForm = New-Object System.Windows.Forms.Form   
  126. $objForm.Text = "Folder Select Form"  
  127. $objForm.Size = New-Object System.Drawing.Size(600,600)   
  128. $objForm.StartPosition = "CenterScreen"  
  129. $tvTreView1 = new-object System.Windows.Forms.TreeView  
  130. $tvTreView1.Location = new-object System.Drawing.Size(1,1)   
  131. $tvTreView1.add_DoubleClick({  
  132.     $Script:clickedFolder = $this.SelectedNode.tag  
  133.     $objForm.Close()  
  134. })  
  135. $tvTreView1.size = new-object System.Drawing.Size(580,580)   
  136. $tvTreView1.Anchor = "Top,left,Bottom"  
  137. [void]$tvTreView1.Nodes.Add($TNRoot)   
  138. $objForm.controls.add($tvTreView1)  
  139. $objForm.ShowDialog()  
  140.   
  141. $clickedfolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId($Script:clickedFolder)     
  142.   
  143. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::DeletedItems,$MailboxName)     
  144. $DuplicatesFolder = New-Object Microsoft.Exchange.WebServices.Data.Folder -ArgumentList $service  
  145. $DuplicatesFolder.DisplayName = "DuplicateItems-Deduped-" + (Get-Date).ToString("yyyy-MM-dd-hh-mm-ss")  
  146. $DuplicatesFolder.Save($folderid)  
  147.   
  148. #Define ItemView to retrive just 1000 Items  
  149. $PidTagSearchKey = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x300B, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  150. $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  151. $psPropset.add($PidTagSearchKey)  
  152.   
  153. $dupHash = @{}  
  154.   
  155. #Create Collection for Move Batch  
  156. $Itemids = @()  
  157. $script:allChoice = $false  
  158.   
  159. $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)      
  160. $ivItemView.PropertySet = $psPropset  
  161. $fiItems = $null      
  162. do{      
  163.     $fiItems = $service.FindItems($clickedfolderid,$ivItemView)      
  164.     #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  165.     foreach($Item in $fiItems.Items){  
  166.         $PropVal =  $null  
  167.         if($Item.TryGetProperty($PidTagSearchKey,[ref]$PropVal)){  
  168.             $SearchString = [System.BitConverter]::ToString($PropVal).Replace("-","")  
  169.             if($dupHash.ContainsKey($SearchString)){  
  170.                 #Check the recivedDate if availible  
  171.                 if($Item.DateTimeReceived -ne $null){  
  172.                     if($Item.DateTimeReceived -eq $dupHash[$SearchString]){  
  173.                         if($script:allChoice -eq $false){  
  174.                             $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""  
  175.                             $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""  
  176.                             $all = new-Object System.Management.Automation.Host.ChoiceDescription "&All","";  
  177.                             $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no,$all)  
  178.                             $message = "Duplicated Detected : Subject " + $Item.Subject + " : Received-" + $dupHash[$SearchString] + " : Created-" + $Item.DateTimeCreated  
  179.                             $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)  
  180.                             if($result -eq 0) {                       
  181.                                 $Itemids += $Item                         
  182.                             }  
  183.                             else{  
  184.                                 if($result -eq 2){  
  185.                                     $script:allChoice = $true  
  186.                                     $Itemids += $Item  
  187.                                 }  
  188.                             }  
  189.                         }  
  190.                         else{  
  191.                             $Itemids += $Item  
  192.                         }  
  193.                     }                     
  194.                 }else{  
  195.                     "Duplicate Found : " + $Item.Subject  
  196.                     $Itemids += $Item  
  197.                 }  
  198.             }  
  199.             else{  
  200.                 "Procesing Item " + $Item.Subject  
  201.                 if($Item.DateTimeReceived -ne $null){  
  202.                     $dupHash.add($SearchString,$Item.DateTimeReceived)  
  203.                 }  
  204.                 else{  
  205.                     $dupHash.add($SearchString,"")  
  206.                 }  
  207.             }  
  208.         }          
  209.     }      
  210.     $ivItemView.Offset += $fiItems.Items.Count      
  211. }while($fiItems.MoreAvailable -eq $true)   
  212.   
  213. #Total Items Processed Varible  
  214. $nmbProcessed = 0  
  215. if($Itemids.Count -gt 0){  
  216.     write-host ("Move " + $Itemids.Count + " Items")  
  217.     #Create Collection for Move Batch  
  218.     $type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"  
  219.     $type = $type.MakeGenericType("Microsoft.Exchange.WebServices.Data.ItemId" -as "Type")  
  220.     $BatchItemids = [Activator]::CreateInstance($type)  
  221.     #Varible to Track BatchSize  
  222.     $batchSize = 0  
  223.     foreach($iiID in $Itemids){  
  224.         $nmbProcessed++  
  225.         $BatchItemids.Add($iiID.Id)  
  226.         if($iiID.Size -ne $null){  
  227.             $batchSize += $iiID.Size  
  228.         }  
  229.         #if BatchCount greator then 50 or larger the 10 MB Move Batch  
  230.         if($BatchItemids.Count -eq 50 -bor $batchSize -gt (10*1MB)){  
  231.             $Result = $null  
  232.             $Result = $service.MoveItems($BatchItemids,$DuplicatesFolder.Id)  
  233.             [INT]$collectionCount = 0  
  234.             [INT]$Rcount = 0    
  235.             [INT]$Errcount = 0  
  236.             $type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"  
  237.             $type = $type.MakeGenericType("Microsoft.Exchange.WebServices.Data.ItemId" -as "Type")  
  238.             #Define Collection to Retry Move For faild Items  
  239.             if($Result -ne $null){  
  240.                 foreach ($res in $Result){   
  241.                     if ($res.Result -eq [Microsoft.Exchange.WebServices.Data.ServiceResult]::Success){    
  242.                         $Rcount++    
  243.                     }   
  244.                     else{  
  245.                         $Errcount++  
  246.                     }  
  247.                     $collectionCount++  
  248.                 }    
  249.             }  
  250.             else{  
  251.                 Write-Host -foregroundcolor red ("Move Result Null Exception")  
  252.             }  
  253.             Write-host ($Rcount.ToString() + " Items Moved Successfully " + "Total Processed " + $nmbProcessed + " Total Folder Items " + $Itemids.Count)   
  254.             if($Errcount -gt 0){  
  255.                 Write-Host -foregroundcolor red ($Errcount.ToString() + " Error failed Moved")  
  256.             }  
  257.             $BatchItemids.Clear()  
  258.             $batchSize = 0  
  259.         }  
  260.     }  
  261.     if($BatchItemids.Count -gt 0){  
  262.         $type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"  
  263.         $type = $type.MakeGenericType("Microsoft.Exchange.WebServices.Data.ItemId" -as "Type")  
  264.         $RetryBatchItemids = [Activator]::CreateInstance($type)  
  265.         $Result = $service.MoveItems($BatchItemids,$DuplicatesFolder.Id)   
  266.         [INT]$Rcount = 0    
  267.         [INT]$Errcount = 0   
  268.         foreach ($res in $Result){    
  269.             if ($res.Result -eq [Microsoft.Exchange.WebServices.Data.ServiceResult]::Success){    
  270.                 $Rcount++    
  271.             }    
  272.             else{  
  273.                 $Errcount++  
  274.             }  
  275.         }    
  276.         Write-host ($Rcount.ToString() + " Items Moved Successfully")  
  277.   
  278.         if($Errcount -gt 0){  
  279.             Write-Host -foregroundcolor red ($Errcount.ToString() + " Error failed Moved")  
  280.         }  
  281.     }  
  282. }  
  283. $DuplicatesFolder.Load()  
  284. if($DuplicatesFolder.TotalCount -eq 0){  
  285.     $DuplicatesFolder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)  
  286. }  





12 comments:

Gavin Palmer said...

Hi Glen,

Hopefully you get this comment. I am desperately in need of a solution like this. I have a scenario on a production environment where Exchange 2007 and Enterprise Vault 8.0.2 are in place. We are migrating to EX2010SP3 and EV10.0.3. The migration steps are Export users Vault to mailbox on 2007, move mailbox to EX2010 using powershell then reprovision on EV10.0.3. Only problem is, there are masses of duplicate items in users vault. Not duplicate vaults, but duplicate email items. I tried your script but i get an error from the very beginning saying:

"Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path 'C:\Program Files\Mi
crosoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll' because it does not exist."

It looks like EWS is not installed. But I thought EWS was all the IIS sites within Exchange. Any help would be appreciated if you have time. Please drop me an email at gavin.palmer@gmail.com.

Thanks

Anonymous said...

Hi Glen

Great script, thanks a lot.

Do you have any idea how it can be adapted to work for a user's In-Place Archive instead of the active mailbox?

Glen Scales said...

Change the line

$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)

to

$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot,$MailboxName)

Cheers
Glen

Peter Stutzinger [dogado] said...

Hi Glen,

thanks for the great script.

Is there any option to go through all mailbox folders recursively?

Glen Scales said...

To recurses all folder in a Mailbox you'll need a code segment like this

#Define Function to convert String to FolderPath
function ConvertToString($ipInputString){
$Val1Text = ""
for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){
$Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))
$clInt++
}
return $Val1Text
}

#Define Extended properties
$PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
#Define the FolderView used for Export should not be any larger then 1000 folders due to throttling
$fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
#Deep Transval will ensure all folders in the search path are returned
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
#Add Properties to the Property Set
$psPropertySet.Add($PR_Folder_Path);
$fvFolderView.PropertySet = $psPropertySet;
#The Search filter will exclude any Search Folders
$sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
$fiResult = $null
#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox
do {
$fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)
foreach($ffFolder in $fiResult.Folders){
$foldpathval = $null
#Try to get the FolderPath Value and then covert it to a usable String
if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))
{
$binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)
$hexArr = $binarry | ForEach-Object { $_.ToString("X2") }
$hexString = $hexArr -join ''
$hexString = $hexString.Replace("FEFF", "5C00")
$fpath = ConvertToString($hexString)
}
"FolderPath : " + $fpath
#Process folder here
}
$fvFolderView.Offset += $fiResult.Folders.Count
}while($fiResult.MoreAvailable -eq $true)

Anonymous said...

Doesn't Work, I can be reached at bruce.ellefritz@phoenix.gov

Exchange 2010 SP2 with Exchange Web Services Managed API 1.2 installed on one of our CAS servers and PS 2.0. I get the following errors:

Exception calling "AutodiscoverUrl" with "2" argument(s): "The Autodiscover service couldn't be located."
At D:\PSscripts\FindDups.ps1:70 char:25
+ $service.AutodiscoverUrl <<<< ($MailboxName,{$true})
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Using CAS Server :
Exception calling "Bind" with "2" argument(s): "The Url property on the ExchangeService object must be set."
At D:\PSscripts\FindDups.ps1:87 char:67
+ $rfRootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind <<<< ($service,$folderidcnt)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Exception calling "FindFolders" with "3" argument(s): "The Url property on the ExchangeService object must be set."
At D:\PSscripts\FindDups.ps1:109 char:37
+ $fiResult = $Service.FindFolders <<<< ($folderidcnt,$sfSearchFilter,$fvFolderView)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At D:\PSscripts\FindDups.ps1:112 char:85
+ $TNChild = new-object System.Windows.Forms.TreeNode($ffFolder.DisplayName.ToString <<<< ())
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\PSscripts\FindDups.ps1:113 char:49
+ $TNChild.Name = $ffFolder.DisplayName.ToString <<<< ()
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\PSscripts\FindDups.ps1:114 char:49
+ $TNChild.Text = $ffFolder.DisplayName.ToString <<<< ()
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\PSscripts\FindDups.ps1:115 char:48
+ $TNChild.tag = $ffFolder.Id.UniqueId.ToString <<<< ()
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: node"
At D:\PSscripts\FindDups.ps1:118 char:27
+ [void]$TNRoot.Nodes.Add <<<< ($TNChild)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At D:\PSscripts\FindDups.ps1:119 char:48
+ $Treeinfo.Add($ffFolder.Id.UniqueId.ToString <<<< (),$TNChild)
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Cancel
New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.Exchange.WebServices.Data.FolderId.
At D:\PSscripts\FindDups.ps1:148 char:30
+ $clickedfolderid = new-object <<<< Microsoft.Exchange.WebServices.Data.FolderId($Script:clickedFolder)
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

Plus more errors than this.

Glen Scales said...

Only the first error is relevant the other are just cascading

Exception calling "AutodiscoverUrl" with "2" argument(s): "The Autodiscover service couldn't be located."

This generally means two things the first is the credentials you entered aren't working(you need to make sure you use the UPN for the useraccount your using and the password not the domain/username format). The other possibility is some Autodiscover config issue to cater for that you can remark these lines like

#CAS URL Option 1 Autodiscover
#$service.AutodiscoverUrl($MailboxName,{$true})
#"Using CAS Server : " + $Service.url

and then unremark the following lines and enter your CAS servername in

#CAS URL Option 2 Hardcoded

$uri=[system.URI] "https://casservername/ews/exchange.asmx"
$service.Url = $uri

Also if you want to use the currently logged on credentials then remark the following line out

#$psCred = Get-Credential
#$creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
#$service.Credentials = $creds

and unremark

#Credentials Option 2
$service.UseDefaultCredentials = $true

Cheers
Glen

Anonymous said...

Love the script but I don't want to be presented with a "select folder" window. How do I modify it to just scan the entire mailbox, all folders, all everything, find all duplicates and wax them? Preferably I'd like a permanent delete option here so I don't have to clean out the recycle bin.

Thanks,
Jeff

Glen Scales said...

Take a look at http://eightwone.com/2013/06/21/removing-duplicate-items-from-a-mailbox/

Greg Redl said...
This comment has been removed by the author.
Greg Redl said...

I am getting the same error as you when running the script from the EMS. Were you able to get it working?

Enrique Fuentes said...

Hi Greg, very well script. But not deleted duplicate.