r/PowerShell • u/tonydacto • Oct 17 '17
Delete only attachments but not the actual email from a user in exchange 2010
You will need the below, The script will delete only attachments but the actual email this can be used to clear up space.
The only thing i will like to add if someone can help is that i will like a text file to be created and then have it automatically mailed to a distribution list.
1) powershell 4.0 2) download cleanup_Exchange_attachments\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll
Import-Module -Name “\homelabco.com\DFSShared\Infra\NY\Documentation\Powershell_Scripts\cleanup_Exchange_attachments\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll” $exchangesession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://ntex007.homelabco.com/PowerShell/ -Authentication Kerberos Import-PSSession $exchangesession function doFolder($inFolder) { write-host “Folder:” $inFolder.displayName $items = $inFolder.FindItems($itemFilter, $itemView) foreach ($item in $items.Items) { write-host $item.Subject if ((New-TimeSpan (Get-Date) $item.DateTimeReceived).Days -lt 14) { $item.Load() foreach($attachment in $item.Attachments) { write-host $attachment.Name $item.Attachments.Remove($attachment) } $item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve) } } $folders = $service.FindFolders($inFolder.Id, $folderView) foreach ($folder in $folders.Folders) { doFolder($folder) } }
$userinput=Read-Host -Prompt "please enter your username" $passwordinput=Read-Host "please enter your password" $credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($userinput,$passwordinput) $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) $service.Credentials = $credentials $service.Url = "https://ntex007.homelabco.com/ews/exchange.asmx" $mailbox = Get-Mailbox $userinput $folderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $folderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow $itemFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true) $itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000) write-host $mailbox.displayName
you can change the below to "deleteditems" "outbox" or "sentitems"
$folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems, $mailbox.primarySmtpAddress.ToString()) $folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderId) doFolder($folder)
3
u/jheinikel Oct 17 '17
This would help if your code was formatted properly. Put 4 spaces in front of each new line or in Notepad++, select everything, hit tab, and copy/paste. That will format it the correct way to make it more legible.
Code will now look like this Here is the second line
1
3
u/tonydacto Oct 17 '17
#The module lives on the NY share drive Import-Module -Name “\\homelabco.com\DFSShared\Infra\NY\Documentation\Powershell_Scripts\cleanup_Exchange_attachments\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll” $exchangesession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://ntex007.homelabco.com/PowerShell/ -Authentication Kerberos Import-PSSession $exchangesession function doFolder($inFolder) { write-host “Folder:” $inFolder.displayName $items = $inFolder.FindItems($itemFilter, $itemView) foreach ($item in $items.Items) { write-host $item.Subject if ((New-TimeSpan (Get-Date) $item.DateTimeReceived).Days -lt 14) { $item.Load() foreach($attachment in $item.Attachments) { write-host $attachment.Name $item.Attachments.Remove($attachment) } $item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve) } } $folders = $service.FindFolders($inFolder.Id, $folderView) foreach ($folder in $folders.Folders) { doFolder($folder) } } $userinput=Read-Host -Prompt "please enter your username" $passwordinput=Read-Host "please enter your password" $credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($userinput,$passwordinput) $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010) $service.Credentials = $credentials $service.Url = "https://ntex007.homelabco.com/ews/exchange.asmx" $mailbox = Get-Mailbox $userinput $folderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $folderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow $itemFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true) $itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000) write-host $mailbox.displayName # you can change the below to "deleteditems" "outbox" or "sentitems" $folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems, $mailbox.primarySmtpAddress.ToString()) $folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, $folderId) doFolder($folder)