summaryrefslogtreecommitdiff
path: root/userphotos/Sync-AD-to-SQLServer.ps1
blob: e3af0d62bd578e61e82cb5d7b80685c106c8f4da (plain)
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
# File: Apply-Photos-From-SharePoint.ps1
# Locations: \\util201\scripts\Associate Photos\test\
# Author: bgstack15
# Startdate: 2019-12-04
# Title: Script that Downloads photos from AD and uploads to SQLServer
# Purpose:
# History:
# Usage:
# References:
#    Refactored to generate array first based on https://stackoverflow.com/questions/27259665/powershell-combine-get-aduser-results
# Improve:
# Dependencies:
#    \\util201\scripts\Functions\userphotolib.ps1
#    \\util201\scripts\Functions\loglib.ps1

# MAIN

# Load libraries
. \\util201\scripts\Functions\userphotolib.ps1

#Config Parameters
$Outdir = "\\util201\e$\scripts\Associate Photos\test\upload-to-sql"
$global:logfile = "$Outdir\..\log\sync-ad-to-sql-${global:today}.log"
$global:logid = Get-Random
$PermittedChanges = 10000
Remove-Variable ErrorMessage -ErrorAction SilentlyContinue

Write-Host "${GLOBAL:Logfile} is the logfile"
Log "Start Sync-AD-to-SQLServer"

if ($true -eq $true)
{
	$Changes = 0
	
	# List all users that have a thumbnailphoto
	$Users = @()
	$Users += Get-ADUser -Filter { thumbnailphoto -Like '*' } -Properties sAMAccountName | Sort-Object -Property sAMAccountName

	# Iterate over each user
	Try
	{
		ForEach ($thisUser in $Users)
		{
			if ( $Changes -Ge $PermittedChanges )
			{
				$ErrorMessage = "Reached maximum amount of changes for this execution: $Changes"
				Log "ERROR: $ErrorMessage"
				break
			} else {
				$outfile="${Outdir}\" + $thisUser.samaccountname + ".jpg"
				Get-Photo-AD -User $thisUser.samaccountname -Filename "$outfile"
				$result = Set-Photo-SQLServer -User $thisUser.samaccountname -Filename "$outfile"
				if ($result -Eq 0){$Changes += 1}
				Remove-Item -Path "$outfile"
			}
		}
	}
	Catch
	{
		if ($_.Exception.Message.Contains("System error."))
		#if ($true -eq $false)
		{
			# suppress the breakexception
		}
		else
		{
			Throw $_
		}

	}
}

Log "Made $Changes changes."
Log "End Sync-AD-to-SQLServer"
If ($ErrorMessage -ne $null) { Throw $ErrorMessage }
bgstack15