How to automate movement of messages between folders in mailbox, Exchange 2010
I had wrong configured content filter on Exchange 2010. So I have a lot of messages in Junk Mail folders on more than 400+ mailboxes. So I need to move these messages into Inbox folder. I didn't find how to automate such operation. I found such problem only in thread: http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/43582019-caaf-4011-8f0d-68871d46ea12 Its not resolved And I have one idea: move messages from "Junk mail" using "export-mailbox" cmdlet to some temp mailbox, then move back to folder "Inbox". Repeat this action on every mailbox. Maybe there is easiest way? Any suggestions?
October 18th, 2010 3:25pm

Thanks a lot. I rewrote the script and it works great for one mailbox, but I was confronted with difficulties when was trying to apply it for each mailbox. I have 3 variants of the script. There is one of them: $StartDate = [system.DateTime]::Today.AddDays(-30) $EndDate = [system.DateTime]::Today $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll" [void][Reflection.Assembly]::LoadFile($dllpath) $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">" $aceuser = [ADSI]$sidbind $service.AutodiscoverUrl($aceuser.mail.ToString()) write-host "Moving messages from Junk Mail to Inbox" ForEach ($mbx in Get-Mailbox -Database Shops) { $MailboxName = $mbx.PrimarySmtpAddress.ToString() $MailboxName $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName) $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) $folderid2 = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail,$MailboxName) $JunkEmailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid2) $Sfgt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $StartDate) $Sflt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $EndDate) $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And); $sfCollection.add($Sfgt) $sfCollection.add($Sflt) $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000) $frFolderResult = $JunkEmailFolder.FindItems($sfCollection,$view) foreach ($miMailItems in $frFolderResult.Items){ "Moving " + $miMailItems.Subject.ToString() [VOID]$miMailItems.Move("Inbox") } } But I always have errors: Exception calling "Bind" with "2" argument(s): "The specified object was not found in the store." At C:\psscript\move_junk_to_inbox.ps1:15 char:66 + $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind <<<< ($service,$folderid) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException Exception calling "Bind" with "2" argument(s): "The specified folder could not be found in the store." At C:\psscript\move_junk_to_inbox.ps1:17 char:70 + $JunkEmailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind <<<< ($service,$folderid2) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException MCP
Free Windows Admin Tool Kit Click here and download it now
October 21st, 2010 3:34am

Does the account your running the script under have full access rights to the mailboxes you're trying to open?[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
October 21st, 2010 8:46am

Thanks for tips. I really didn't have fullaccess rights to this mailboxes. So the problem was solved. This is my code with comments: # #Do not use remote exchange powershell. There it doesn't work! #Add snappin (for Exchange 2010): Add-snappin Microsft.Exchange.Management.Powershell.E2010 # #Before using please test! #TestMode=1 - test mode is on, 0 - off # $TestMode=0 $TestMailboxName="example@example.com" # Messages for the last 30 days will be moved $StartDate = [system.DateTime]::Today.AddDays(-30) $EndDate = [system.DateTime]::Today #To run this script you need to install EWS API (version 1.1 in this example) $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll" [void][Reflection.Assembly]::LoadFile($dllpath) $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">" $aceuser = [ADSI]$sidbind $service.AutodiscoverUrl($aceuser.mail.ToString()) function MoveMails ($MailboxName) { # #Change "-User admin" # Add-MailboxPermission -Identity $MailboxName -User admin -Accessright Fullaccess $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName) $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) "Checking"+$MailboxName+" in "+$InboxFolder.DisplayName $folderid2 = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail,$MailboxName) $JunkEmailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid2) $Sfgt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $StartDate) $Sflt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $EndDate) $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And); $sfCollection.add($Sfgt) $sfCollection.add($Sflt) $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000) $frFolderResult = $JunkEmailFolder.FindItems($sfCollection,$view) foreach ($miMailItems in $frFolderResult.Items){ "Moving " + $miMailItems.Subject.ToString() [VOID]$miMailItems.Move("Inbox") } clear-variable -name mailboxname } if ($TestMode -eq 1) { "Test Mode" MoveMails $TestMailboxName } else { Get-mailbox -database shops | foreach { $_.PrimarySmtpAddress.ToString() MoveMails $_.PrimarySmtpAddress.ToString() } } MCP
Free Windows Admin Tool Kit Click here and download it now
October 25th, 2010 2:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics