Link to home
Start Free TrialLog in
Avatar of blackmores
blackmoresFlag for Australia

asked on

Need to delete email from all users mailboxes

I have an email that has been sent to everyone in the company. I want to delete this email from everyone's computer to minimise damage. A log of who it deleted from, and copies of the emails in a dedicated mailbox would be great too.

Our environment is Exchange 2010 SP1, so that means Exhcange 2007 or Exchange 2010 process for doing this is different.

I have tried running the following commands but had little success.
Get-Mailbox -Server "*SERVER*" | Search-Mailbox -TargetMailbox "*SEARCHACCOUNT*" -TargetFolder "*EMAILSDELETED*" -SearchQuery 'Subject:"*SUBJECT CONTAINS THIS*" AND Body:"*BODY CONTAINS THIS*"' -deletecontent

Open in new window

Get-Mailbox -Server  “*SERVER*” | Search-Mailbox -SearchQuery ‘Subject:”*SUBJECT CONTAINS THIS*” Body:"*BODY CONTAINS THIS*"‘ -DeleteContent

Open in new window


- I have been running the command from the enterprise admin credentials.
- The first command returned this result:
The input object cannot be bound to any parameters for the command either because the command does not take pipeline in
put or the input and its properties do not match any of the parameters that take pipeline input.
    + CategoryInfo          : InvalidArgument: (*MAILBOXNAME*:PSObject) [Search-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Search-Mailbox

Open in new window


-The second command returned a similar response.
-When i run the command without the deletecontent switch, but include targetmailbox i can see it finds a number of these emails.

Any help would be appreicated.
Ivan
ASKER CERTIFIED SOLUTION
Avatar of Akhater
Akhater
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great advice and nice article from Antoine that should serve you well
Avatar of blackmores

ASKER

Hi Akhater,

I rant through the steps you suggested. Below is the command and the output i recieved when i ran the permissions step:

[PS] C:\Windows\system32>New-ManagementRoleAssignment -Name "Import Export Mailbox Admins" -SecurityGroup "Exchange Mail
box Support" -Role "Mailbox Import Export"

Name                           Role              RoleAssigneeName  RoleAssigneeType  AssignmentMethod  EffectiveUserNam
                                                                                                       e
----                           ----              ----------------  ----------------  ----------------  ----------------
Import Export Mailbox Admins   Mailbox Import... Exchange Mailb... SecurityGroup     Direct

Then i ran the following command as per your suggestion:
[PS] C:\Windows\system32>$mbxes = Get-Mailbox -Server  "*SERVER*" -resultsize unlimited
[PS] C:\Windows\system32>$mbxes | Search-Mailbox -SearchQuery 'Subject:"*SUBJECT CONTAINS THIS*" Body:"*BODY CONTAINS THIS*"
' -DeleteContent

Open in new window

I also tried:
[PS] C:\Windows\system32>Get-Mailbox -Server  "*SERVER*" -resultsize unlimited | Search-Mailbox -SearchQuery 'Subject:"*SUBJECT CONTAINS THIS*" Body:"*BODY CONTAINS THIS*"' -DeleteContent

For both, this was the output i received:
The target mailbox or .pst file path is required.
    + CategoryInfo          : InvalidArgument: (:) [], ArgumentException
    + FullyQualifiedErrorId : 78CD297B

Invoke-Command : Cannot write input as there are no more running pipelines
At C:\Users\administrator.BLACKMORES\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\*SERVER.DOMAIN*\*SERVER*.domain.psm1:42991 char:29
+             $scriptCmd = { & <<<<  $script:InvokeCommand `
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
    + FullyQualifiedErrorId : NoMoreInputWrite,Microsoft.PowerShell.Commands.InvokeCommandCommand

Cheers,
Ivan

P.S. Your article was a good read, and very straight forward. Will be noting that one down.
it seems you have an issue with the  'Subject:"*SUBJECT CONTAINS THIS*" Body:"*BODY CONTAINS THIS*"'  part


try to replace it by  '"Subject:*SUBJECT CONTAINS THIS*" and "Body:*BODY CONTAINS THIS*"'

just make sure it is a ' followed by a " in the start and " followed by a ' at the end
Hi All,
Here are two commands i tried, and the two returns i recieved:
[PS] C:\Windows\system32>Get-Mailbox -Server  "EXCHANGE" -resultsize unlimited | Search-Mailbox -SearchQuery 'Subject:"RE: Mobile Phones" Body:"0400 000000"' -DeleteContent
The target mailbox or .pst file path is required.
    + CategoryInfo          : InvalidArgument: (:) [], ArgumentException
    + FullyQualifiedErrorId : 78CD297B

Invoke-Command : Cannot write input as there are no more running pipelines
At C:\Users\administrator.BLACKMORES\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\*SERVER.DOMAIN*\*SERVER*.domain.psm1:42991 char:29
+             $scriptCmd = { & <<<<  $script:InvokeCommand `
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
    + FullyQualifiedErrorId : NoMoreInputWrite,Microsoft.PowerShell.Commands.InvokeCommandCommand

Open in new window


And

[PS] C:\Windows\system32>Get-Mailbox -Server  "EXCHANGE" -resultsize unlimited | Search-Mailbox -SearchQuery '"Subject:RE: Mobile Phones" "Body:0400 000000"' -DeleteContent
The target mailbox or .pst file path is required.
    + CategoryInfo          : InvalidArgument: (:) [], ArgumentException
    + FullyQualifiedErrorId : 78CD297B

Invoke-Command : Cannot write input as there are no more running pipelines
At C:\Users\administrator.BLACKMORES\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\*SERVER.DOMAIN*\*SERVER*.domain.psm1:42991 char:29
+             $scriptCmd = { & <<<<  $script:InvokeCommand `
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
    + FullyQualifiedErrorId : NoMoreInputWrite,Microsoft.PowerShell.Commands.InvokeCommandCommand

Open in new window


Note the difference between the two is the inverted commas. I also typed the keys directly into the console so its not incorrect markup from a text editor

Any help appreciated.
ivan
Performing further tests, i tired the following command based on infomation o got here and get a different error:
[PS]Get-Mailbox -Server  "EXCHANGE" -resultsize unlimited | Search-Mailbox  -targetmailbox "mymailbox" -targetfolder "deletecommand" -SearchQuery 'Subject:"RE: Mobile Phones" Body:"0400 000000"' -DeleteContent

The input object cannot be bound to any parameters for the command either because the command does not take pipeline in
put or the input and its properties do not match any of the parameters that take pipeline input.
    + CategoryInfo          : InvalidArgument: (CS Temp2:PSObject) [Search-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Search-Mailbox

Open in new window


Hope that helps,
Ivan
please try this

Get-Mailbox -Server  "EXCHANGE" -resultsize unlimited | Search-Mailbox -SearchQuery '"Subject:RE: Mobile Phones" and "Body:0400 000000"' -DeleteContent
The result as expected was:
The input object cannot be bound to any parameters for the command either because the command does not take pipeline in
put or the input and its properties do not match any of the parameters that take pipeline input.
    + CategoryInfo          : InvalidArgument: (USER:PSObject) [Search-Mailbox], ParameterBindingException

Open in new window


I have tried to simplify things, so i tried the following command:
[PS] Search-Mailbox -identity "ivan" -targetmailbox "admin" -targetfolder "deletecommand" -Searc
hQuery "Subject:'Ivan Test Delete Email Through Exchange'"  -LogOnly -LogLevel Full

Open in new window

The above worked successfully!

I tried the -DeleteContent switch and that too worked as expected.

The problem is that the initial Get-Mailbox -Server  "EXCHANGE" -resultsize unlimited does not appear to be passing through the user names down the pipe (as the error suggests).

If i run
Search-Mailbox -SearchQuery '"Subject:RE: Mobile Phones" and "Body:0400 000000"' -DeleteContent

Open in new window

or similar, it prompts to input an identity.

Regards,
Ivan



Correction, the -LogOnly -LogLevel Full works successfully
the -DeleteContent comes up with the following error:
A positional parameter cannot be found that accepts argument '-deletecontent'.
    + CategoryInfo          : InvalidArgument: (:) [Search-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Search-Mailbox

Open in new window

Type search-mailbox -delte(press tab) does it autocomplete ?

If not then you have a permission issue did you follow steps in my article?
@Akhater, i have checked the recommendation and it did NOT resolve.
i assumed that it was a type (search-mailbox -delte) and instead i tried (search-mailbox -delet) and after pressing TAB it resolved.

Attached is the screenshot of the command:
Get-ManagementRoleAssignment -Role "mailbox import export"

The user that im running the command with is part of that group.

Regards,
Ivan
exchange1.jpg
I just entered the following command and it worked successfully:
Search-Mailbox -identity "ivand" -SearchQuery "Subject:'Ivan Test Delete Email Through Exchange'"  -deletecontent

I think the problem is when i what to apply it to multiple mailboxes.

Ivan
since you have now the -DeleteContent then you assigned the permission correctly

for multiple mailboxes please follow my first post


$mbxes = Get-Mailbox -Server  “*SERVER*” -resultsize unlimited

$mbxes | Search-Mailbox -SearchQuery ‘Subject:”*SUBJECT CONTAINS THIS*” Body:"*BODY CONTAINS THIS*"‘ -DeleteContent

thanks
@Akhater,

The problem is resolved. I beleive the resolution was your permissions issue, however it must have taken some time to propegate as it did not work imediately.

Thanks for your help.
Nice blog.
tx for the update and for the points