-
Notifications
You must be signed in to change notification settings - Fork 583
/
FindPotentialDirectoryProblems.PS1
147 lines (131 loc) · 6.47 KB
/
FindPotentialDirectoryProblems.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# FindPotentialDirectoryProblems.PS1
#
# Quick and dirty script to highlight potential issues which might exist in an Office 365 tenant directory
#
# Get list of user mailboxes in the directory
If ($Null -eq (Get-ConnectionInformation)) {
Connect-ExchangeOnline -SkipLoadingCmdletHelp
}
$Report = [System.Collections.Generic.List[Object]]::new() # Create output file
Write-Host "Finding User mailboxes..."
[array]$Directory = Get-User -RecipientTypeDetails UserMailbox -ResultSize Unlimited
If (!($Directory)) {
Write-Host "Unable to find user accounts - exiting" ; break
}
# Find people without an office and other potential directory problems
[array]$NoOffice = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Office))}
ForEach ($C in $NoOffice) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Office" }
$Report.Add($ReportLine)
}
[array]$NoPhone = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Phone))}
ForEach ($C in $NoPhone) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Phone" }
$Report.Add($ReportLine)
}
[array]$NoCity = $Directory | Where-Object {([string]::IsNullOrEmpty($_.City))}
ForEach ($C in $NoCity) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No City" }
$Report.Add($ReportLine)
}
$NoCompany = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Company))}
ForEach ($C in $NoCompany) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Company" }
$Report.Add($ReportLine)
}
[array]$NoState = $Directory | Where-Obbject {([string]::IsNullOrEmpty($_.StateOrProvince))}
ForEach ($C in $NoState) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No State" }
$Report.Add($ReportLine)
}
[array]$NoManager = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Manager))}
ForEach ($C in $NoManager) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Manager" }
$Report.Add($ReportLine)
}
$NoZip = $Directory | Where-Object {([string]::IsNullOrEmpty($_.PostalCode))}
ForEach ($C in $NoZip) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Postal code" }
$Report.Add($ReportLine)
}
[array]$NoTitle = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Title))}
ForEach ($C in $NoTitle) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Title" }
$Report.Add($ReportLine)
}
[array]$NoStreet = $Directory | Where-Object {([string]::IsNullOrEmpty($_.StreetAddress))}
ForEach ($C in $NoStreet) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Street Address" }
$Report.Add($ReportLine)
}
# No department
[array]$NoDepartment = $Directory | Where-Object {([string]::IsNullOrEmpty($_.Department))}
ForEach ($C in $NoDepartment) {
$ReportLine = [PSCustomObject] @{
User = $C.UserPrincipalName
Name = $C.DisplayName
Issue = "No Department" }
$Report.Add($ReportLine)
}
# Calculate percentages
$PercentNoOffice = ($NoOffice.Count/$Directory.Count).ToString("P")
$PercentNoCity = ($NoCity.Count/$Directory.Count).ToString("P")
$PercentNoCompany = ($NoCompany.Count/$Directory.Count).ToString("P")
$PercentNoState = ($NoState.Count/$Directory.Count).ToString("P")
$PercentNoManager = ($NoManager.Count/$Directory.Count).ToString("P")
$PercentNoZip = ($NoZip.Count/$Directory.Count).ToString("P")
$PercentNoTitle = ($NoTitle.Count/$Directory.Count).ToString("P")
$PercentNoPhone = ($NoPhone.Count/$Directory.Count).ToString("P")
$PercentNoStreet = ($NoStreet.Count/$Directory.Count).ToString("P")
$PercentNoDepartment = ($NoDepartment.count/$Directory.count).toString("P")
Clear-Host
Write-Host " "
Write-Host ("Number of user mailboxes {0}" -f $Directory.Count)
Write-Host "---------------------------"
Write-Host " "
Write-Host "Analysis of missing directory properties"
Write-Host "----------------------------------------"
Write-Host ("Number of mailboxes with no Office {0} ({1})" -f $NoOffice.Count, $PercentNoOffice)
Write-Host ("Number of mailboxes with no City {0} ({1})" -f $NoCity.Count, $PercentNoCity)
Write-Host ("Number of mailboxes with no Company {0} ({1})" -f $NoCompany.Count, $PercentNoCompany)
Write-Host ("Number of mailboxes with no Department {0} ({1})" -f $NoDepartment.count, $PercentNoDepartment)
Write-Host ("Number of mailboxes with no State {0} ({1})" -f $NoState.Count, $PercentNoState)
Write-Host ("Number of mailboxes with no Manager {0} ({1})" -f $NoManager.Count, $PercentNoManager)
Write-Host ("Number of mailboxes with no Title {0} ({1})" -f $NoTitle.Count, $PercentNoTitle)
Write-Host ("Number of mailboxes with no Phone {0} ({1})" -f $NoPhone.Count, $PercentNoPhone)
Write-Host ("Number of mailboxes with no Address {0} ({1})" -f $NoStreet.Count, $PercentNoStreet)
Write-Host ("Number of mailboxes with no Post Code {0} ({1})" -f $NoZip.Count, $PercentNoZip)
$Report | Sort-Object User | Export-CSV c:\temp\DirectoryIssues.csv -NoTypeInformation
Write-Host " "
Write-Host "An output file containing details of missing directory properties is available in c:\temp\DirectoryIssues.csv"
# 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.practical365.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.