-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMailboxAutoReplyConfiguration.ps1
More file actions
276 lines (253 loc) · 14.1 KB
/
GetMailboxAutoReplyConfiguration.ps1
File metadata and controls
276 lines (253 loc) · 14.1 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<#
=============================================================================================
Name: Export Office 365 mailbox users' OOF configuration status
Description: This script exports Office 365 mailbox users' OOF configuration status to CSV
Version: 1.0
Website: o365reports.com
Script Highlights:
~~~~~~~~~~~~~~~~~
1. Generates 5 different types of out of office set up status reports
2. Automatically installs the Exchange Online module upon your confirmation when it is not available in your machine
3. Lists aggregated result of users with enabled and scheduled auto-reply set up
4. Retrieves users who have enabled auto-reply settings
5. Generates users who configured scheduled out of office set up separately
6. Lists currently unavailable users’ out of office configurations
7. You have an option to get the disabled mailboxes with active automatic reply setting
8. Delivers Office 365 users’ upcoming out of office plans details
9. Supports both MFA and Non-MFA accounts
10. Exports the report in CSV format
11. The script is scheduler-friendly. You can automate the report generation upon passing credentials as parameters.
For detailed Script execution: https://o365reports.com/2021/08/18/get-mailbox-automatic-reply-configuration-using-powershell
============================================================================================
#>
param (
[string] $UserName = $null,
[string] $Password = $null,
[Switch] $Enabled,
[Switch] $Scheduled,
[Switch] $DisabledMailboxes,
[Switch] $Today,
[String] $ActiveOOFAfterDays
)
#Checks ExchangeOnline module availability and connects the module
Function ConnectToExchange {
$Exchange = (get-module ExchangeOnlineManagement -ListAvailable).Name
if ($Exchange -eq $null) {
Write-host "Important: ExchangeOnline PowerShell module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
$confirm = Read-Host Are you sure you want to install module? [Y] Yes [N] No
if ($confirm -match "[yY]") {
Write-host "Installing ExchangeOnlineManagement"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Write-host "ExchangeOnline PowerShell module is installed in the machine successfully."`n
}
elseif ($confirm -cnotmatch "[yY]" ) {
Write-host "Exiting. `nNote: ExchangeOnline PowerShell module must be available in your system to run the script."
Exit
}
}
#Storing credential in script for scheduling purpose/Passing credential as parameter
if (($UserName -ne "") -and ($Password -ne "")) {
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName, $SecuredPassword
Connect-ExchangeOnline -Credential $Credential -ShowProgress $false | Out-Null
}
else {
Connect-ExchangeOnline | Out-Null
}
Write-Host "ExchangeOnline PowerShell module is connected successfully"`n
#End of Connecting Exchange Online
}
#This function checks the user choice and retrieves the OOF status
Function RetrieveOOFReport {
#Checks the users with scheduled OOF setup
if ($Scheduled.IsPresent) {
$global:ExportCSVFileName = "OOFScheduledUsersReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-mailbox -ResultSize Unlimited | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -eq "Scheduled" }
if ($null -ne $CurrOOFConfigData ) {
PrepareOOFReport
ExportScheduledOOF
}
}
}
#Checks the OOF status on and after user mentioned days
elseif ($ActiveOOFAfterDays -gt 0) {
$global:ExportCSVFileName = "UpcomingOOFStatusReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
$OOFStartDate = (Get-date).AddDays($ActiveOOFAfterDays).Date.ToString().split(" ") | Select -Index 0
Get-mailbox -ResultSize Unlimited | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -ne "Disabled" }
if ($null -ne $CurrOOFConfigData ) {
$CurrOOFStartDate = $CurrOOFConfigData.StartTime.ToString().split(" ") | select -Index 0
$CurrOOFEndDate = $CurrOOFConfigData.EndTime.ToString().split(" ") | Select -Index 0
$ActiveOOFAfterFlag = "true"
if($CurrOOFConfigData.AutoReplyState -eq "Enabled" -or ($OOFStartDate -ge $CurrOOFStartDate -and $OOFStartDate -le $CurrOOFEndDate)){
PrepareOOFReport
ExportAllActiveOOFSetup
}
}
}
}
#Checks the OOF with enabled status
elseif ($Enabled.IsPresent) {
$global:ExportCSVFileName = "OOFEnabledUsersReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-mailbox -ResultSize Unlimited | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -eq "Enabled" }
if ($null -ne $CurrOOFConfigData ) {
$EnabledFlag = 'true'
PrepareOOFReport
ExportEnabledOOF
}
}
}
#Checks whether OOF starting day is current day and process
elseif ($Today.Ispresent) {
$global:ExportCSVFileName = "OOFUsersTodayReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
$CurrDate = (Get-Date).Date.ToString().split(" ") | Select -Index 0
Get-mailbox -ResultSize Unlimited | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -ne "Disabled" }
if ($null -ne $CurrOOFConfigData ) {
$CurrOOFStartDate = $CurrOOFConfigData.StartTime.ToString().split(" ") | select -Index 0
$CurrOOFEndDate = $CurrOOFConfigData.EndTime.ToString().split(" ") | Select -Index 0
if ($CurrDate -ge $CurrOOFStartDate -and $CurrDate -le $CurrOOFEndDate) {
PrepareOOFReport
ExportAllActiveOOFSetup
}
}
}
}
#Checks the disabled mailoxes OOF configuration
elseif ($DisabledMailboxes.Ispresent) {
$global:ExportCSVFileName = "DisabledAccountsOOFConfigurationReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-mailbox -ResultSize Unlimited | Where-Object { $_.AccountDisabled -eq $TRUE } | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -ne "Disabled" }
if ($null -ne $CurrOOFConfigData ) {
PrepareOOFReport
ExportDisabledMailboxOOFSetup
}
}
}
#Checks the all active OOF configuration
else {
$global:ExportCSVFileName = "OutofOfficeConfigurationReport-" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-mailbox -ResultSize Unlimited | foreach-object {
$CurrUser = $_
$CurrOOFConfigData = Get-MailboxAutoReplyConfiguration -Identity ($CurrUser.PrimarySmtpAddress) | Where-object { $_.AutoReplyState -ne "Disabled" }
if ($null -ne $CurrOOFConfigData ) {
PrepareOOFReport
ExportAllActiveOOFSetup
}
}
}
}
#Checks the Boolean values
Function GetPrintableValue($RawData) {
if ($null -eq $RawData -or $RawData.Equals($false)) {
return "No"
}
else {
return "Yes"
}
}
#Saves the users with OOF configuration
Function PrepareOOFReport {
$global:ReportSize = $global:ReportSize + 1
$EmailAddress = $CurrUser.PrimarySmtpAddress
$AccountStatus = $CurrUser.AccountDisabled
$MailboxOwner = $CurrOOFConfigData.MailboxOwnerId
$OOFStatus = $CurrOOFConfigData.AutoReplyState
$AutoCancelRequests = GetPrintableValue $CurrOOFConfigData.AutoDeclineFutureRequestsWhenOOF
$CancelAllEvents = GetPrintableValue $CurrOOFConfigData.DeclineAllEventsForScheduledOOF
$CancelScheduledEvents = GetPrintableValue $CurrOOFConfigData.DeclineEventsForScheduledOOF
$CreateOOFEvent = GetPrintableValue $CurrOOFConfigData.CreateOOFEvent
$ExternalAudience = $CurrOOFConfigData.ExternalAudience
$StartTime = $CurrOOFConfigData.StartTime
$EndTime = $CurrOOFConfigData.EndTime
$Duration = $EndTime - $StartTime
$TimeSpan = "$($Duration.Days.ToString('00'))d : $($Duration.Hours.ToString('00'))h : $($Duration.Minutes.ToString('00'))m";
if ($CurrOOFConfigData.InternalMessage -ne "") {
$InternalMessage = (($CurrOOFConfigData.InternalMessage) -replace '<.*?>', '').Trim()
}
else { $InternalMessage = "-" }
if ($CurrOOFConfigData.ExternalMessage -ne "") {
$ExternalMessage = (($CurrOOFConfigData.ExternalMessage) -replace '<[^>]+>', '').Trim()
}
else { $ExternalMessage = "-" }
if ($EnabledFlag -eq 'true') {
$global:OOFDuration = 'OOF Duration'
}
else {
$global:OOFDuration = 'OOF Duration (Days:Hours:Mins)'
}
if ($OOFStatus -eq 'Enabled') {
$StartTime = "-"
$EndTime = "-"
$TimeSpan = 'Until auto-reply is disabled'
}
Write-Progress "Retrieving the OOF Status of the User: $MailboxOwner" "Processed Users Count: $global:ReportSize"
#Save values with output column names
$ExportResult = @{
'Email Address' = $EmailAddress;
'Disabled Account' = $AccountStatus;
'Mailbox Owner' = $MailboxOwner;
'Auto Reply State' = $OOFStatus;
'Start Time' = $StartTime;
'End Time' = $EndTime;
'Decline Future Requests' = $AutoCancelRequests;
'Decline All Events' = $CancelAllEvents;
'Decline Scheduled Events' = $CancelScheduledEvents;
'Create OOF Event' = $CreateOOFEvent;
'External Audience' = $ExternalAudience;
'Internal Message' = $InternalMessage;
'External Message' = $ExternalMessage;
$global:OOFDuration = $TimeSpan
}
$global:ExportResults = New-Object PSObject -Property $ExportResult
}
#Exports the users with OOF schedued configuration
Function ExportScheduledOOF {
$global:ExportResults | Select-object 'Mailbox Owner', 'Email Address', 'Start Time', 'End Time', $global:OOFDuration, 'Decline Future Requests', 'Decline All Events', 'Decline Scheduled Events', 'Create OOF Event', 'External Audience', 'Internal Message', 'External Message', 'Disabled Account' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
#Exports the users with OOF Enabled configuration
Function ExportEnabledOOF {
$global:ExportResults | Select-object 'Mailbox Owner', 'Email Address', $global:OOFDuration, 'External Audience', 'Internal Message', 'External Message', 'Disabled Account' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
#Exports all the users with OOF configuration
Function ExportAllActiveOOFSetup {
if($ActiveOOFAfterFlag = "true"){
$global:ExportResults | Select-object 'Mailbox Owner', 'Email Address', 'Auto Reply State', 'Start Time', 'End Time', $global:OOFDuration, 'External Audience', 'Internal Message', 'External Message','Disabled Account' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
else{
$global:ExportResults | Select-object 'Mailbox Owner', 'Email Address', 'Auto Reply State', 'Start Time', 'End Time', $global:OOFDuration, 'External Audience', 'Internal Message', 'External Message', 'Decline Future Requests', 'Decline All Events', 'Decline Scheduled Events', 'Create OOF Event', 'Disabled Account' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
}
Function ExportDisabledMailboxOOFSetup {
$global:ExportResults | Select-object 'Mailbox Owner', 'Email Address', 'Auto Reply State', 'Start Time', 'End Time', $global:OOFDuration, 'Decline Future Requests', 'Decline All Events', 'Decline Scheduled Events', 'Create OOF Event', 'External Audience', 'Internal Message', 'External Message' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
#Execution starts here
ConnectToExchange
$global:ReportSize = 0
RetrieveOOFReport
#Validates the output file
if ((Test-Path -Path $global:ExportCSVFileName) -eq "True") {
#Open file after code execution finishes
Write-Host " The output file available in:"-NoNewline -ForegroundColor Yellow; Write-Host $global:ExportCSVFileName
Write-host `n"Exported $global:ReportSize records to CSV." `n
Write-Host "Disconnected active ExchangeOnline session" `n
Write-Host ~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline;
Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$prompt = New-Object -ComObject wscript.shell
$userInput = $prompt.popup("Do you want to open output file?", 0, "Open Output File", 4)
If ($userInput -eq 6) {
Invoke-Item "$global:ExportCSVFileName"
}
}
else {
Write-Host "No data found with the specified criteria"
}
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue