/
jssldapsync_ps.ps1
289 lines (227 loc) · 8.79 KB
/
jssldapsync_ps.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# JAMF Building/Department Syncronization with AD
# Converted from https://github.com/jamfit/JSS-LDAP-Sync by Matt Bobke
# Outputs desired content to the console (and to a log file, if the switch is given)
[CmdletBinding()]
Param(
[Switch]$LogFile
)
# Logging-related variables in script scope
$Script:LogFile = $LogFile
$Script:LogFolderPath = ".\Logs"
$Script:LogFilePath = $LogFolderPath + "\" + (Get-Date -Format FileDate) + ".txt"
function Log ([String]$Content) {
Write-Host $Content
if ($Script:LogFile) {Add-Content -Path $Script:LogFilePath -Value $Content}
}
# Returns a list of all departments in JAMF.
function GetDepartments ([String]$JssUrl, [PSCredential]$Credential) {
$listDepartments = @{}
$requestResponse = Invoke-WebRequest -Uri "$JssUrl/departments" -Credential $Credential
try {
$root = [Xml]$requestResponse.Content
$departmentNodes = $root.SelectNodes("//department")
}
catch {
# There were no departments stored in JAMF
$departmentNodes = $null
}
if ($departmentNodes -ne $null) {
foreach ($node in $departmentNodes) {
$listDepartments.Add($node.name, $null)
}
}
$listDepartments
}
# Returns a list of all buildings in JAMF.
function GetBuildings ([String]$JssUrl, [PSCredential]$Credential) {
$listBuildings = @{}
$requestResponse = Invoke-WebRequest -Uri "$JssUrl/buildings" -Credential $Credential
try {
$root = [Xml]$requestResponse.Content
$buildingNodes = $root.SelectNodes("//building")
}
catch {
# There were no buildings stored in JAMF
$buildingNodes = $null
}
if ($buildingNodes -ne $null) {
foreach ($node in $buildingNodes) {
$listBuildings.Add($node.name, $null)
}
}
$listBuildings
}
# Creates a department in JAMF from the $Name parameter.
function CreateDepartment ([String]$JssUrl, [PSCredential]$Credential, [String]$Name) {
$name = [System.Security.SecurityElement]::Escape($Name) # Replaces invalid XML characters
$body = "<department><name>$name</name></department>"
$webRequestParams = @{
Uri = "$JssUrl/departments/id/0";
Credential = $Credential;
Method = "Post";
Body = $body;
ContentType = 'application/xml; charset=utf-8';
}
$response = Invoke-WebRequest @webRequestParams
}
# Creates a building in JAMF from the $Name parameter.
function CreateBuilding ([String]$JssUrl, [PSCredential]$Credential, [String]$Name) {
$name = [System.Security.SecurityElement]::Escape($Name) # Replaces invalid XML characters
$body = "<building><name>$name</name></building>"
$webRequestParams = @{
Uri = "$JssUrl/buildings/id/0";
Credential = $Credential;
Method = "Post";
Body = $body;
ContentType = 'application/xml; charset=utf-8';
}
$response = Invoke-WebRequest @webRequestParams
}
# Deletes a department from JAMF that matches the $Name parameter.
function DeleteDepartment ([String]$JssUrl, [PSCredential]$Credential, [String]$Name) {
$webRequestParams = @{
Uri = "$JssUrl/departments/name/$Name";
Credential = $Credential;
Method = "Delete";
}
$response = Invoke-WebRequest @webRequestParams
}
# Deletes a building from JAMF that matches the $Name parameter.
function DeleteBuilding ([String]$JssUrl, [PSCredential]$Credential, [String]$Name) {
$webRequestParams = @{
Uri = "$JssUrl/buildings/name/$Name";
Credential = $Credential;
Method = "Delete";
}
$response = Invoke-WebRequest @webRequestParams
}
# Gathers lists of all unique departments and buildings that exist in LDAP user records.
function GetLdapLists ([String]$SearchBase, [String]$LdapServer, [PSCredential]$Credential) {
# Return variables
$departments = @{}
$buildings = @{}
$getADUserParams = @{
Filter = {(Enabled -eq $True) -and (ObjectClass -eq "User")};
Properties = "Department", "StreetAddress";
Server = $LdapServer;
Credential = $Credential;
}
$staff = Get-ADUser @getADUserParams
foreach ($user in $staff) {
try {
$department = $user.Department
if (!$departments.ContainsKey($department)) {
$departments.Add($department, $null)
}
}
catch {
continue
}
try {
$building = $user.StreetAddress
if (!$buildings.ContainsKey($building)) {
$buildings.Add($building, $null)
}
}
catch {
continue
}
}
$departments, $buildings
}
# Returns:
# A list of items that exist in LDAP but not JSS (to be created in JSS).
# A list of items that exist in JSS but not LDAP (to be deleted from JSS).
function CompareLists ([HashTable]$LdapList, [HashTable]$JssList) {
$toCreate = @{}
$toDelete = @{}
foreach ($i in $LdapList.Keys) {
if (!$JssList.ContainsKey($i)) {
$toCreate.Add($i, $null)
}
}
foreach ($i in $JssList.Keys) {
if (!$LdapList.ContainsKey($i)) {
$toDelete.Add($i, $null)
}
}
$toCreate, $toDelete
}
# Begin script execution
$LdapServer = ""
$JssUrl = ""
# LDAP/JSS Credentials - assumed to be the same
# Uncomment the below block and enter username/password for autorun
<# $Username = ""
$PasswordUnencrypted = ""
$SecureStringParams = @{
String = $PasswordUnencrypted;
AsPlainText = $True;
Force = $True;
}
$Password = ConvertTo-SecureString @SecureStringParams # Enforcing SecureString type for password #>
# The staff OU (AD Distinguished Name) that will be used (assumed to also contain the authenticating user)
# Example: "OU=Users,DC=contoso,DC=com"
$StaffOU = ""
# Delete 5th oldest log file (if it exists) and create a new one.
if ($Script:LogFile) {
if (!(Test-Path -Path $Script:LogFolderPath)) {
New-Item -ItemType Directory -Force -Path $Script:LogFolderPath | Out-Null
}
Get-ChildItem -Path $Script:LogFolderPath | `
Where-Object -FilterScript {-not $_.PSIsContainer} | `
Sort-Object -Property CreationTime -Descending | `
Select-Object -Skip 4 | `
Remove-Item -Force | `
Out-Null
New-Item -ItemType File -Path $Script:LogFilePath -Force | Out-Null
}
if (!$LdapServer) {
$LdapServer = Read-Host -Prompt "LDAP Server"
}
if (!$JssUrl) {
$JssUrl = Read-Host -Prompt "JSS URL"
}
$JssUrl = $JssUrl + "/JSSResource"
if (!$Username) {
$Username = Read-Host -Prompt "Username"
}
if (!$Password) {
$Password = Read-Host -AsSecureString -Prompt "Password"
}
# PSCredential object for storing credential
$UserCredential = [PSCredential]::New($Username, $Password)
Log "Reading accounts in $StaffOU in LDAP..."
$LdapDepartments, $LdapBuildings = GetLdapLists $StaffOU $LdapServer $UserCredential
Log "$($LdapDepartments.Count) department(s) and $($LdapBuildings.Count) building(s) exist in LDAP."
Log "Getting JSS departments and buildings..."
$JssDepartments = GetDepartments $JssUrl $UserCredential
$JssBuildings = GetBuildings $JssUrl $UserCredential
$JssCreateDepartments, $JssDeleteDepartments = CompareLists $LdapDepartments $JssDepartments
Log ("$($JssCreateDepartments.Count) department(s) will be created and " +
"$($JssDeleteDepartments.Count) department(s) will be deleted in the JSS.")
$JssCreateBuildings, $JssDeleteBuildings = CompareLists $LdapBuildings $JssBuildings
Log ("$($JssCreateBuildings.Count) building(s) will be created and " +
"$($JssDeleteBuildings.Count) building(s) will be deleted in the JSS.")
foreach ($Department in $JssCreateDepartments.Keys) {
Log "Creating department: $Department"
CreateDepartment $JssUrl $UserCredential $Department
}
foreach ($Department in $JssDeleteDepartments.Keys) {
Log "Deleting department: $Department"
DeleteDepartment $JssUrl $UserCredential $Department
}
foreach ($Building in $JssCreateBuildings.Keys) {
Log "Creating building: $Building"
CreateBuilding $JssUrl $UserCredential $Building
}
foreach ($Building in $JssDeleteBuildings.Keys) {
Log "Deleting building: $Building"
DeleteBuilding $JssUrl $UserCredential $Building
}
# CSV Output - uncomment to output lists of departments/buildings created/deleted
<# $JssCreateDepartments.GetEnumerator() | Export-Csv -Path ".\CreateDepartments.csv" -Delimiter ',' -NoTypeInformation
$JssDeleteDepartments.GetEnumerator() | Export-Csv -Path ".\DeleteDepartments.csv" -Delimiter ',' -NoTypeInformation
$JssCreateBuildings.GetEnumerator() | Export-Csv -Path ".\CreateBuildings.csv" -Delimiter ',' -NoTypeInformation
$JssDeleteBuildings.GetEnumerator() | Export-Csv -Path ".\DeleteBuildings.csv" -Delimiter ',' -NoTypeInformation #>
Log "Complete!"