forked from ascivv/DLActivityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLMonthlyInactivityReport-Public.ps1
More file actions
163 lines (128 loc) · 6.4 KB
/
DLMonthlyInactivityReport-Public.ps1
File metadata and controls
163 lines (128 loc) · 6.4 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
<#
.NOTES
===========================================================================
Updated on: 8/31/2018
Created by: /u/ascIVV
===========================================================================
Exchange Online Powershell is required for message trace.
https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps
ReportHTML Moduile is required
Install-Module -Name ReportHTML
https://www.powershellgallery.com/packages/ReportHTML/
.DESCRIPTION
Compare four weeks of DLWeeklyInactivity report results from your O365 tenant. Removes Weekly reports older than 5 weeks, sends detailed HTML report on unused distribution lists.
#>
#Connection info
$Username = "admin.account@domain.com"
$PasswordPath = "\\path\to\secure\password.txt"
#Read the password from the file and convert to SecureString
$SecurePassword = Get-Content $PasswordPath | ConvertTo-SecureString
#Build a Credential Object from the password file and the $username constant
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
#Open a session to O365
$ExOSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
import-PSSession $ExOSession -AllowClobber
#Set the constants
$Date = get-date -format MMddyyyy
$ReportsFolder = "\\server\share\reports\"
$CompanyLogo = "https://www.freelogodesign.org/Content/img/logo-ex-4.png"
$Table = New-Object 'System.Collections.Generic.List[System.Object]'
$RemovedFilesTable = New-Object 'System.Collections.Generic.List[System.Object]'
#Get report run date for previous weekly reports
$Week1Date = (get-date).AddDays(-21).ToString("MMddyyyy")
$Week2Date = (get-date).AddDays(-14).ToString("MMddyyyy")
$Week3Date = (get-date).AddDays(-7).ToString("MMddyyyy")
$Week4Date = (get-date).ToString("MMddyyyy")
#Clean up weekly reports created more than 35 days ago
$ToOldFiles = ("$ReportsFolder"+"Inactive*"+".txt")
$FilestoRemove = Get-ChildItem -Path $ToOldFiles -Force | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-35) }
Foreach ($File in $FilestoRemove) {
Remove-Item $File -Force
if (!$?) {
$ReportName = "$($File.name)"
$Status = "Failed to delete the file automatically."
}
Else {
$ReportName = "$($File.name)"
$Status = "Successfully deleted the file automatically."
}
$obj = [PSCustomObject]@{
'File Name' = $ReportName
'Deleted Status' = $Status
}
$RemovedFilesTable.add($obj)
}
If (($RemovedFilesTable).count -eq 0)
{
$RemovedFilesTable = [PSCustomObject]@{
'Information' = 'Information: No Inactive Weekly Lists were found to remove.'
}
}
#Set up the weekly report file paths
$Week1Path = ("$ReportsFolder"+"Inactive"+"$Week1Date"+".txt")
$Week2Path = ("$ReportsFolder"+"Inactive"+"$Week2Date"+".txt")
$Week3Path = ("$ReportsFolder"+"Inactive"+"$Week3Date"+".txt")
$Week4Path = ("$ReportsFolder"+"Inactive"+"$Week4Date"+".txt")
#Input weekly report files
$Week1Report = Get-Content $Week1Path
$Week2Report = Get-Content $Week2Path
$Week3Report = Get-Content $Week3Path
$Week4Report = Get-Content $Week4Path
#Compare weekly report files
$Week12Results = Compare-Object -ReferenceObject $Week1Report -DifferenceObject $Week2Report -ExcludeDifferent -IncludeEqual
$Week23Results = Compare-Object -ReferenceObject $Week12Results.InputObject -DifferenceObject $Week3Report -ExcludeDifferent -IncludeEqual
$Week34Results = Compare-Object -ReferenceObject $Week23Results.InputObject -DifferenceObject $Week4Report -ExcludeDifferent -IncludeEqual
#Filter slider object out of the results
$MonthlyInactive = $Week34Results.InputObject
#Set export file name for plain text file to be used by quarterly report
$DLMonthlyActivityTxt = ("$ReportsFolder"+"MonthlyInactive"+"$date"+".txt")
#Export the findings to the text file
$MonthlyInactive | Out-File $DLMonthlyActivityTxt
#Get inactive distribution list details and create HTML report
Foreach ($List in $MonthlyInactive) {
$ListDetails = get-DistributionGroup $List
$DisplayName = $ListDetails.DisplayName
$Email = $ListDetails.PrimarySMTPAddress
$Synced = $ListDetails.IsDirSynced
$Owner = ($ListDetails.ManagedBy) -join ", "
$Members = (Get-DistributionGroupMember $List | Sort-Object Name | Select-Object -ExpandProperty Name) -join ", "
$MeasureMembers = $Members | measure
$NumberofMembers = $MeasureMembers.count
$obj = [PSCustomObject]@{
'Name' = $DisplayName
'Email Address' = $Email
'AD Synced' = $Synced
'Owners' = $Owner
'Members' = $Members
}
$Table.add($obj)
}
If (($Table).count -eq 0)
{
$Table = [PSCustomObject]@{
'Information' = 'Information: No distribution lists are inactive.'
}
}
$rpt = New-Object 'System.Collections.Generic.List[System.Object]'
$rpt += get-htmlopenpage -TitleText 'Monthly Inactive Distribution List Report' -LeftLogoString $CompanyLogo
$rpt += Get-HTMLContentOpen -HeaderText "Distribution lists that have not been emailed in 4 weeks."
$rpt += get-htmlcontentdatatable $Table -HideFooter
$rpt += Get-HTMLContentClose
$rpt += Get-HTMLContentOpen -HeaderText "Weekly Inactive Reports not created in the past 5 weeks."
$rpt += get-htmlcontentdatatable $RemovedFilesTable -HideFooter
$rpt += Get-HTMLContentClose
$rpt += Get-HTMLClosePage
$rpt += Get-HTMLClosePage
$ReportName = ("DLMonthlyInactiveReport" + "$Date")
Save-HTMLReport -ReportContent $rpt -ShowReport -ReportName $ReportName -ReportPath $ReportsFolder
$MonthlyReport = ("$ReportsFolder"+"$ReportName"+".html")
#Send an email with the findings
$From = "admin.account@domain.com"
$To = "helpdesk@domain.com"
$Subject = "Monthly Inactive Distribution List Report"
$Body = "See the attached report for distribution lists that have not been emailed in the past 4 weeks. A .txt file has been saved in the file share to be accessed by the Quarterly DL Inactivity Report script. Do not modify any of the weekly or monthly .txt file master copies in the share."
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credential -Attachments $MonthlyReport
#Close the session to O365
Remove-PSSession $ExOSession