-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetArchiveMailboxSize.ps1
More file actions
200 lines (184 loc) · 7.52 KB
/
GetArchiveMailboxSize.ps1
File metadata and controls
200 lines (184 loc) · 7.52 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
<#
=============================================================================================
Name: Exchange Online Archive MAilbox Size Report
Description: This script exports Exchange Online Archive Mailbox sizes to CSV
Version: 1.0
Website: o365reports.com
Script Highlights:
~~~~~~~~~~~~~~~~~
1.Validates and installs Exchange Online PowerShell module upon user confirmation (if not exists already)
2.Facility to supply the input and export the output in the CSV format
3.The report is delivered with the significant attributes. As it is the user–friendly script, you can publish desired attributes too.
4.Supports both MFA and Non-MFA accounts
5.The script is scheduler-friendly. You can schedule the report generation upon giving UserName and Password.
For detailed script execution: https://o365reports.com/2021/03/30/export-office-365-archive-mailbox-size-report-using-powershell
============================================================================================
#>
param
(
[string] $UserName = $null,
[string] $Password = $null,
[Switch] $UserMBOnly,
[Switch] $SharedMBOnly,
[Switch] $AutoExpandingArchiveEnabled,
[String] $MBIdentityFile
)
function CSVImport {
$IdentityList = Import-Csv -Header "IdentityValue" $MBIdentityFile
foreach($MailboxDetails in $IdentityList) {
$currIdentity = $MailboxDetails.IdentityValue
if($null -eq $WhereObjectCheck)
{
$UserData = Get-Mailbox -Identity $currIdentity -Archive -ErrorAction SilentlyContinue
}
else
{
$UserData = Get-Mailbox -Identity $currIdentity -Archive -ErrorAction SilentlyContinue | Where-Object $WhereObjectCheck
}
if($null -eq $UserData)
{
Write-Host $currIdentity mailbox is not archive enabled/Invalid.
}
else
{
ExportOutput
}
}
}
function ExportOutput {
$ArchiveMailboxSize = ((Get-MailboxStatistics -Identity $UserData.UserPrincipalName -Archive -WarningAction SilentlyContinue).TotalItemSize)
if($null -ne $ArchiveMailboxSize)
{
$ArchiveMailboxSize = $ArchiveMailboxSize.ToString().split("()")
$ArchiveMailboxSizeRounded = $ArchiveMailBoxSize | Select-Object -Index 0
$ArchiveMailboxSizeBytes = ($ArchiveMailBoxSize | Select-Object -Index 1).ToString().Split(" ") | Select-Object -Index 0
}
else
{
$ArchiveMailboxSizeRounded = $ArchiveMailboxSizeBytes = "0"
}
if($UserData.AutoExpandingArchiveEnabled -eq $True)
{
$AutoExpandArchive = "Enabled"
}
else
{
$AutoExpandArchive = "Disabled"
}
$CurrExportValue = $UserData.DisplayName
$ArchiveQuotaSize = ($UserData.ArchiveQuota).ToString().split("()") | Select-Object -Index 0
$ArchiveWarningQuotaSize = ($UserData.ArchiveWarningQuota).ToString().split("()") | Select-Object -Index 0
$global:ReportSize = $global:ReportSize + 1
Write-Progress -Activity "Exporting $CurrExportValue`n Processed Mailbox count: $global:ReportSize" -Status "Preparing Report"
#Export output to CSV
$ExportResult = @{'DisplayName' = $UserData.DisplayName; 'Email Address' = $UserData.UserPrincipalName; 'Recipient Type' = $UserData.RecipientTypeDetails; 'Archive Name' = $UserData.ArchiveName; 'Archive Mailbox Size' = $ArchiveMailboxSizeRounded; 'Archive Mailbox Size(Bytes)' = $ArchiveMailboxSizeBytes; 'Archive Quota' = $ArchiveQuotaSize; 'Archive Warning Quota'= $ArchiveWarningQuotaSize; 'Archive Status' = $UserData.ArchiveStatus; 'Archive State'= $UserData.ArchiveState; 'Auto Expanding Archive' = $AutoExpandArchive }
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-Object 'DisplayName', 'Email Address', 'Recipient Type', 'Archive Name', 'Archive Mailbox Size', 'Archive Mailbox Size(Bytes)', 'Archive Quota','Archive Warning Quota','Archive Status','Archive State', 'Auto Expanding Archive' | Export-csv -path $ExportCSVFileName -NoType -Append
}
#Checking Exchange Online PowerShell Module Availability
$Exchange = (get-module ExchangeOnlineManagement -ListAvailable).Name
if($Exchange -eq $null)
{
Write-host "Important: Module ExchangeOnline 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 "Required Module is installed in the machine Successfully"
}
else
{
Write-host "Exiting. `nNote: Exchange 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
}
Write-Host "Exchange PowerShell Connected Successfully"
#End of Connecting Exchange Online
$ExportCSVFileName = ".\ArchiveMailboxSizeReport_$((Get-Date -format MMM-dd` hh-mm` tt).ToString()).csv"
Write-Host Generating report... `n
#filtering the conditions based on the User Input param
$WhereObjectCheck = $null
if($UserMBOnly.IsPresent -or $SharedMBOnly.IsPresent)
{
$RecipientFilterVal = 'UserMailbox'
if($SharedMBOnly.IsPresent)
{
$RecipientFilterVal = 'SharedMailbox'
}
if($AutoExpandingArchiveEnabled.IsPresent)
{
$WhereObjectCheck = { ($_.RecipientTypeDetails -eq $RecipientFilterVal) -and ($_.AutoExpandingArchiveEnabled -eq $true) }
}
else
{
$WhereObjectCheck = { $_.RecipientTypeDetails -eq $RecipientFilterVal }
}
}
elseif($AutoExpandingArchiveEnabled.IsPresent)
{
$WhereObjectCheck = { $_.AutoExpandingArchiveEnabled -eq $true }
}
$global:ReportSize =0
#Check for input file
if ([string]$MBIdentityFile -ne "")
{
CSVImport
}
#Generating all Archive Mailbox Size report
else
{
if ($null -eq $WhereObjectCheck)
{
$AllValidMailboxes = Get-Mailbox -ResultSize Unlimited -Archive
}
#Generating Specific Archive Mailbox Size report on checking Where condition
else
{
$AllValidMailboxes = Get-Mailbox -ResultSize Unlimited -Archive | Where-Object $WhereObjectCheck
}
foreach($UserData in $AllValidMailboxes)
{
ExportOutput
}
}
<#if($global:ReportSize -eq 0)
{
Write-Host "There is no Archive Mailbox to return"
}
else
{
Write-Host "The output file contains $global:ReportSize mailboxes."`n
}
#>
#Open output file after execution
if((Test-Path -Path $ExportCSVFileName) -eq "True")
{
Write-Host "The output file contains $global:ReportSize mailboxes."`n
Write-Host " The Output file available in:" -NoNewline -ForegroundColor Yellow
Write-Host $ExportCSVFileName
$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 "$ExportCSVFileName"
}
}
else
{
Write-Host "There is no Archive Mailbox to return"
}
Disconnect-ExchangeOnline -Confirm:$false | Out-Null
Write-Host `n~~ 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