-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve-ad-pictures.ps1
47 lines (41 loc) · 1.83 KB
/
retrieve-ad-pictures.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
Import-Module ActiveDirectory
if (Test-Path "users.txt") {
$DirectoryToCreate = "Photos"
if (-not (Test-Path -LiteralPath $DirectoryToCreate)) {
try {
New-Item -Path $DirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch {
Write-Error -Message "Unable to create directory '$DirectoryToCreate'. Error was: $_" -ErrorAction Stop
}
Write-Output "Successfully created directory '$DirectoryToCreate'."
}
Import-Csv users.txt | Foreach-Object {
foreach ($property in $_.PSObject.Properties)
{
$user = $null
try {
$user = Get-ADUser -Identity $property.Value -Properties thumbnailPhoto -Server DOMAINSERVER.company.com
} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
Write-Output "Unable to find an user with NT user "+$property.Value
} catch {
Write-Output "Unable to connect to domain controller "+$property.Value
}
if ($user) {
try {
$name = $DirectoryToCreate + "\\" + $user.sAMAccountName + ".jpg"
$user.thumbnailPhoto | Set-Content $name -Encoding byte
if ((Get-Item $name).length -cle 0kb)
{
Write-Output "User "+$user.sAMAccountName+" has no picture"
Remove-Item $name
}
} catch {
Write-Output "Error downloading picture for NT user "+$user.sAMAccountName
}
}
}
}
} else {
Write-Output "You must have a users.txt file on the folder. Please create the file with one NT username line by line"
}