forked from 12Knocksinna/Office365itpros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindInactiveDLs.PS1
36 lines (32 loc) · 1.92 KB
/
FindInactiveDLs.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
# FindInactiveDls
# Find inactive distribution lists based on the message trace informnation, which means we can only go back 7 days...
# https://github.com/12Knocksinna/Office365itpros/blob/master/FindInactiveDLs.PS1
$EndDate = Get-Date
$StartDate = $EndDate.AddDays(-10)
$Messages = $null
$Page = 1
Write-Host "Collecting message trace data for the last 10 days"
Do
{
$CurrMessages = (Get-MessageTrace -Status Expanded -PageSize 5000 -Page $Page -StartDate $StartDate -EndDate $EndDate | Select Received, RecipientAddress)
$Page++
$Messages += $CurrMessages
}
Until ($CurrMessages -eq $Null)
$MessageTable = @{}
$Messagetable = ($Messages | Sort RecipientAddress -Unique | Select RecipientAddress, Received)
$DLs = Get-DistributionGroup -ResultSize Unlimited
Write-Host "Processing" $DLs.Count "distribution lists..."
$Results = ForEach ($DL in $DLs) {
If ($MessageTable -Match $DL.PrimarySMTPAddress) {
[pscustomobject]@{Name = $DL.DisplayName ; Active = "Yes"}
Write-Host $DL.DisplayName "is active" -Foregroundcolor Yellow }
Else {
[pscustomobject]@{Name = $DL.DisplayName ; Active = "No"}
Write-Host $DL.DisplayName "inactive" -Foregroundcolor Red }
}
$Results | Export-CSV c:\Temp\ListofDLs.csv -NoTypeInformation
# An example script used to illustrate a concept. More information about the topic can be found in the Office 365 for IT Pros eBook https://gum.co/O365IT/
# and/or a relevant article on https://office365itpros.com or https://www.petri.com. See our post about the Office 365 for IT Pros repository # https://office365itpros.com/office-365-github-repository/ for information about the scripts we write.
# Do not use our scripts in production until you are satisfied that the code meets the need of your organization. Never run any code downloaded from the Internet without
# first validating the code in a non-production environment.