This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathad-dl.ps1
227 lines (211 loc) · 8.01 KB
/
ad-dl.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
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
<#
.SYNOPSIS
Recursively obtains email addresses from the specified distribution lists
from Active Directory (AD). Works with AD or Office 365 aliases.
.DESCRIPTION
This script queries the infrastructure masters of available domains under
the default AD forest to fetch information of the specified 'ProxyAddresses'
property.
.EXAMPLE
PS C:\> .\ad-dl.ps1 -InFile <ifile>
Read file and print output to console.
.EXAMPLE
PS C:\> .\ad-dl.ps1 -InFile <ifile> -OutFile <ofile>
Read file and print output to file.
.EXAMPLE
PS C:\> Write-Host "<dl-addr1>`n<dl-addr2>" | .\ad-dl.ps1
Pipe data and print output to console.
.EXAMPLE
PS C:\> Get-Content <ifile> | .\ad-dl.ps1
Pipe data data read from a file and print output to console.
.EXAMPLE
PS C:\> Get-Content <ifile> | .\ad-dl.ps1 -OutFile <ofile>
Pipe data read from a file and print output to file.
.NOTES
Author: Diogo Fernandes
URL: https://github.com/diogo-fernan/
Requires: PowerShell v2.0
#>
Param (
[Parameter(
HelpMessage = "Input 'ProxyAddresses' file (one per line).",
Mandatory = $true,
Position = 1,
ValueFromPipeline = $true
)]
# [ValidateScript({ Test-Path $_ })]
[Alias('InFile')]
[string]$ifile,
[Parameter(
HelpMessage = "Output Comma Separated Value (CSV) file.",
Position = 2
)]
[Alias('OutFile')]
[string]$ofile
)
if ($Input) { $data = [string]$Input }
else {
if (Test-Path $ifile) { $data = [string](Get-Content $ifile) }
else { throw [System.IO.FileNotFoundException] "$ifile not found" }
}
if ($data -ne $null) {
import-module activedirectory
$data = $data -split "[\n\s\t,;:]"
$dom = (Get-ADForest).Domains
if ($dom -ne $null) {
$dc = $dom | foreach {(Get-ADDomain $_).InfrastructureMaster}
$data = $data | ? { $_ } | sort -uniq | % { $_.Trim() }
if ($ofile) {
Remove-Item $ofile 2>$null
}
function ArrayToHash ($a) {
begin { $h = [ordered]@{} }
process { $a | foreach { $h[$_] = $null } }
end { return $h }
}
function Run-ADQuery {
Param (
[Parameter(Mandatory=$true)]
[string]$iserver,
[Parameter(Mandatory=$true)]
[string]$istr
)
Begin {
$o = $null
$str = "$($istr.split("@")[0])@"
}
Process {
try {
$g = Get-ADGroup `
-Server $iserver `
-Filter "ProxyAddresses -like '*SMTP:$str*'" `
-Properties DisplayName,ProxyAddresses `
| Select DisplayName,ProxyAddresses
} catch [System.TimeoutException] { }
if ($g) {
$ga = ""
foreach ($i in $g.ProxyAddresses) {
if ($i -like "*SMTP:$str*") {
$ga = "$($ga)$($i.split(":")[1]),"
}
}
$o = New-Object PSObject -Property @{
DisplayName = $g.DisplayName
GroupAddresses = "$istr,$($ga.Substring(0,$ga.Length-1))"
}
$o.GroupAddresses = ($o.GroupAddresses -Split "," | Select -uniq) -join ","
}
}
End { $o }
}
function Query-ADGroupMember {
Param (
[Parameter(Mandatory=$true)]
[string]$iserver,
[Parameter(Mandatory=$true)]
[string]$istr
)
Begin { $o = @(); $oo = $null }
Process {
try {
$oo = Get-ADGroupMember -Server $iserver -Recursive $istr `
| Select -Unique
foreach ($i in $oo) {
try {
$o += Get-ADUser -Server $iserver $i -Properties EmailAddress `
| Select-Object -Expand EmailAddress
} catch [Microsoft.ActiveDirectory.Management.ADReferralException] {
$flag = $True
foreach ($j in $($dc | Where-Object {$_ -ne $iserver})) {
try {
$o += Get-ADUser -Server $j $i -Properties EmailAddress `
| Select-Object -Expand EmailAddress
$flag = $False
break
} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { }
}
if ($flag) {
$o += $i.SamAccountName
}
}
}
} catch [Microsoft.ActiveDirectory.Management.ADException],
[Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException],
[System.TimeoutException] { }
}
End { $o | Sort -uniq }
}
function Query-ADGroup {
Param (
[Parameter(Mandatory=$true)]
[string]$istr
)
Begin { $o = @() }
Process {
foreach ($i in $dc) {
$g = Run-ADQuery $i $istr
if ($g -ne $null) {
break
}
}
if ($g -eq $null) {
$g = New-Object PSObject -Property @{
EmailAddress = ""
GroupName = ""
GroupAddresses = $istr
}
}
if ($g.DisplayName -ne $null -and $g.DisplayName -ne "") {
$g.DisplayName = $g.DisplayName -Replace "^\*",""
foreach ($i in $dc) {
$p = Query-ADGroupMember $i $g.DisplayName
if ($p -ne $null) {
break
}
}
if ($p -eq $null) {
$o += New-Object PSObject -Property @{
EmailAddress = ""
GroupName = ""
GroupAddresses = $g.GroupAddresses
}
} else {
foreach ($i in $p) {
$o += New-Object PSObject -Property @{
EmailAddress = $i
GroupName = $g.DisplayName
GroupAddresses = $g.GroupAddresses
}
}
}
} else {
$o += New-Object PSObject -Property @{
EmailAddress = ""
GroupName = ""
GroupAddresses = $g.GroupAddresses
}
}
}
End { return $o }
}
function Main {
Param (
[Parameter(Mandatory=$true)]
[string[]]$id,
[string]$of
)
$data = @()
foreach ($i in $id) {
$data += Query-ADGroup $i
}
if ($of) {
$data | Export-CSV -Path $of -NoTypeInformation
} else {
$data | Format-List | Out-String
}
}
Main $data $ofile
} else {
Write-Host "no domains found under the default forest."
}
}