-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathRemove-EOMails.ps1
124 lines (108 loc) · 4.89 KB
/
Remove-EOMails.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#Requires -Version 5.0
<#
.SYNOPSIS
Connect to Exchange Online and removes mails from mailbox
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/O365/ExchangeOnline/MailBoxes
.Parameter O365Account
[sr-en] Specifies a account that has permission to perform this action
[sr-de] Benutzerkonto zur Ausführung der Aktion
.Parameter MailboxId
[sr-en] Specifies the user principal name of the mailbox
[sr-de] UPN der Mailbox
.Parameter RemoveOlderDay
[sr-en] Deletes all mails older than X days
[sr-de] Löscht alle Mails, die älter als X Tage sind
.Parameter RemoveOlderThan
[sr-en] Deletes all mails up to this date
[sr-de] Löscht alle Mails, bis zu diesem Zeitpunkt
.Parameter PurgeType
[sr-en] Specifies how to remove items
[sr-de] Gibt an, ob Mails endgültig gelöscht werden
#>
param(
[Parameter(Mandatory = $true)]
[pscredential]$O365Account,
[Parameter(Mandatory = $true)]
[string]$MailboxId,
[int]$RemoveOlderDay = 90,
[Parameter(HelpMessage="ASRDisplay(Date)")]
[datetime]$RemoveOlderThan,
[ValidateSet('SoftDelete','HardDelete')]
[string]$PurgeType = 'SoftDelete'
)
try{
$Script:result = $null
$compSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $O365Account -Authentication Basic -AllowRedirection
$null = Import-PSSession $compSession -AllowClobber
if($null -eq $RemoveOlderThan){
$RemoveOlderThan = (Get-Date).AddDays(-$RemoveOlderDay)
}
[string]$searchName = 'SRPurgeSearch'
try{
if($null -ne (Get-ComplianceSearch -Identity $searchName -ErrorAction SilentlyContinue)){
$null = Remove-ComplianceSearch -Identity $searchName -Confirm:$false -ErrorAction SilentlyContinue
}
$null = New-ComplianceSearch -Name $searchName -ExchangeLocation $MailboxId -ContentMatchQuery "Received<$($RemoveOlderThan.ToString('yyyy-MM-dd'))(c:c)(ItemClass=IPM.Note)"
$job = Start-ComplianceSearch -Identity $searchName -AsJob
try{
while($job.State -eq 'Running'){ # wait until job is finished
Start-Sleep -Seconds 1
}
if($job.State -eq 'Completed'){
Start-Sleep -Seconds 5 # to be on the safe side
$res = (Get-ComplianceSearch -Identity $searchName | Select-Object -ExpandProperty Items)
if($res -gt 0){ # remove mails
$sAct = New-ComplianceSearchAction -SearchName $searchName -Purge -PurgeType $PurgeType -Force -Confirm:$false | Select-Object -ExpandProperty Status
try{
while(($sAct -eq 'Starting') -or ($sAct -eq 'InProgress')){ # wait until job is finished
Start-Sleep -Seconds 1
$sAct = (Get-ComplianceSearchAction -Identity "$($searchName)_Purge" | Select-Object -ExpandProperty Status)
}
if($sAct -eq 'Completed'){ # mails removed
$Script:result = Get-ComplianceSearchAction -Identity "$($searchName)_Purge" | Select-Object -ExpandProperty Results
}
else{
throw "Search action failed"
}
}
finally{
$null = Remove-ComplianceSearchAction -Identity "$($searchName)_Purge" -Confirm:$false -ErrorAction SilentlyContinue
}
}
else{
$Script:result = 'No mails to delete found'
}
}
else{
throw "Search mails failed"
}
}
finally{
$null = Remove-ComplianceSearch -Identity $searchName -Confirm:$false -ErrorAction SilentlyContinue
}
}
finally{
$null = Remove-PSSession -Session $compSession -Confirm:$false
}
if($SRXEnv) {
$SRXEnv.ResultMessage = $Script:result
}
else{
Write-Output $Script:result
}
}
catch{
throw
}
finally{
}