-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDump-UserPhotos.ps1
223 lines (178 loc) · 7.15 KB
/
Dump-UserPhotos.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
function Dump-UserPhotos {
<#
.SYNOPSIS
Dump all the users pictures from Active Directory into a folder.
.DESCRIPTION
This cmdlet allows a normal user, without any special permissions, to
dump all the users pictures from Active Directory into a folder.
Subfolders will be created for each domain.
.PARAMETER DomainFile
File containing the list of domains.
.PARAMETER ResultDirectory
Directory where the files will be dumped.
.LINK
https://www.serializing.me/tags/active-directory/
.EXAMPLE
Dump-UserPhotos -DomainFile .\Domains.xml -$ResultDirectory .\UserPhotos
.NOTE
Function: Dump-UserPhotos
Author: Duarte Silva (@serializingme)
License: GPLv3
Required Dependencies: None
Optional Dependencies: None
Version: 1.0.5
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[String]$DomainFile,
[Parameter(Mandatory = $True)]
[String]$ResultDirectory
)
function BinarySID-ToStringSID {
param(
[Byte[]]$ObjectSID
)
return (New-Object Security.Principal.SecurityIdentifier @(
$ObjectSID, 0)).Value
}
function Process-User {
param(
[IO.DirectoryInfo]$ResultDirectoryInfo,
[Collections.Hashtable]$User
)
if ([String]::IsNullOrEmpty($User.Identifier) -or $User.Photo -eq $Null) {
return
}
[IO.FileStream]$OutputStream = $null
try {
$OutputStream = New-Object -TypeName 'System.IO.FileStream' -ArgumentList @(
[System.IO.Path]::Combine($ResultDirectoryInfo.FullName, ("{0}.jpeg" -f $User.Identifier)),
[IO.FileMode]::Create,
[IO.FileAccess]::Write
)
$OutputStream.Write($User.Photo, 0, $User.Photo.Length)
}
finally {
if ($OutputStream -ne $null) {
$OutputStream.Dispose()
}
}
}
function Process-Domain {
param(
[IO.DirectoryInfo]$ResultDirectoryInfo,
[String]$DomainName,
[String]$DomainDNS
)
Write-Verbose ('Obtaining users in the {0} domain' -f $DomainName)
[DirectoryServices.DirectoryEntry]$DomainRoot = $Null
[DirectoryServices.DirectorySearcher]$UserSearch = $Null
try {
[IO.DirectoryInfo]$DomainDirectoryInfo = New-Object IO.DirectoryInfo @(
[System.IO.Path]::Combine($ResultDirectoryInfo.FullName, $DomainName) )
if ($DomainDirectoryInfo.Exists -eq $False) {
New-Item -Path $ResultDirectoryInfo.FullName -Name $DomainName -Type Directory | Out-Null
}
[DirectoryServices.SortOption]$Sort = New-Object DirectoryServices.SortOption('objectsid',
[DirectoryServices.SortDirection]::Ascending);
$DomainRoot = New-Object DirectoryServices.DirectoryEntry @(
'LDAP://{0}' -f $DomainName )
$UserSearch = New-Object DirectoryServices.DirectorySearcher @(
$DomainRoot, '(&(samaccounttype=805306368)(thumbnailPhoto=*))' )
$UserSearch.PageSize = 500
$UserSearch.Sort = $Sort
$UserSearch.PropertiesToLoad.Add('objectsid') | Out-Null
$UserSearch.PropertiesToLoad.Add('thumbnailPhoto') | Out-Null
[Collections.Hashtable]$User = @{
'Identifier' = $Null;
'Photo' = $Null;
}
$UserSearch.FindAll() | ForEach-Object {
if ($_.Properties.objectsid -ne $Null) {
$User.Identifier = BinarySID-ToStringSID -ObjectSID $_.Properties.objectsid.Item(0)
}
else {
$User.Identifier = 'S-1-0-0'
}
if (($_.Properties.thumbnailphoto -ne $Null) -and
($_.Properties.thumbnailphoto.Item(0) -ne $Null)) {
$User.Photo = $_.Properties.thumbnailphoto.Item(0)
}
Process-User -ResultDirectoryInfo $DomainDirectoryInfo -User $User
$User.Identifier = $Null
$User.Photo = $Null
}
}
catch {
Write-Warning ('Failed to find users for {0} ({1})' -f $DomainName, $DomainDNS)
}
finally {
if ($UserSearch -ne $Null) {
$UserSearch.Dispose()
}
if ($DomainRoot -ne $Null) {
try {
$DomainRoot.Dispose()
}
catch {
Write-Warning 'Failed to dispose the domain root, probably because the server is not operational'
}
}
}
}
[IO.FileStream]$DomainFileStream = $Null
[Xml.XmlReader]$DomainFileReader = $Null
try {
[IO.FileInfo]$DomainFileInfo = New-Object IO.FileInfo @( $DomainFile )
if ($DomainFileInfo.Exists -eq $False) {
Write-Error 'The file to read the domains from does not exist'
}
[IO.DirectoryInfo]$ResultDirectoryInfo = New-Object IO.DirectoryInfo @( $ResultDirectory )
if ($ResultDirectoryInfo.Exists -eq $False) {
New-Item -Path $ResultDirectoryInfo.FullName -Type Directory | Out-Null
}
# Instantiate the XML stream and reader.
$DomainFileStream = New-Object IO.FileStream @( $DomainFileInfo.FullName,
[IO.FileMode]::Open, [IO.FileAccess]::Read )
[Xml.XmlReaderSettings]$DomainXmlSettings = New-Object Xml.XmlReaderSettings
$DomainXmlSettings.IgnoreProcessingInstructions = $True
$DomainXmlSettings.IgnoreComments = $True
$DomainXmlSettings.ProhibitDtd = $True
$DomainFileReader = [Xml.XmlReader]::Create($DomainFileStream, $DomainXmlSettings)
[Collections.HashTable]$Domain = @{
'Name' = $Null;
'DNS' = $Null;
}
while ($DomainFileReader.Read()) {
if ($DomainFileReader.IsStartElement()) {
if (($DomainFileReader.Name -ne 'Domain') -and
($DomainFileReader.Name -ne 'Trusted')) {
continue
}
while ($DomainFileReader.MoveToNextAttribute()) {
if ($DomainFileReader.Name -eq 'Name') {
$Domain.Name = $DomainFileReader.Value
}
elseif ($DomainFileReader.Name -eq 'DNS') {
$Domain.DNS = $DomainFileReader.Value
}
}
Process-Domain -ResultDirectoryInfo $ResultDirectoryInfo -DomainName $Domain.Name `
-DomainDNS $Domain.DNS
}
elseif ($DomainFileReader.Name -eq 'Domain') {
$Domain.Name = $Null
$Domain.DNS = $Null
}
}
}
finally {
if ($DomainFileReader -ne $Null) {
$DomainFileReader.Close()
}
if ($DomainFileStream -ne $Null) {
$DomainFileStream.Close()
}
}
}