-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTD-API-Functions.ps1
More file actions
902 lines (714 loc) · 33.9 KB
/
Copy pathTD-API-Functions.ps1
File metadata and controls
902 lines (714 loc) · 33.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
#####################################################################
#
# Script: TD-API-Functions.ps1
#
# Contains a series of functions used for working with AD accounts
# And the TDX API
#
# Author: dparr@granite-it.net
# Created: Sept. 2021
#
#
#####################################################################
#Create LogFile. Can be statically set to a specific path
$Global:Logfile = read-host "Key In full path to log file"
#Global Variables for Team Dynamix API Integration
#Unique to Your Institution
$Global:apiUser = 'td_api_username'
$Global:apiPW = 'SuperSecretPassword'
$Global:BEID = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
$Global:WebServicesID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
$Global:contentType = 'application/json'
$Global:tdURL = 'https://td.acmeinc.ca/TDWebApi/api'
$Global:app_id = 55 #Unique to Your Institution
$Global:UUIDs = @() #all TD Person UUIDs get added to this variable so that they can be have a desktop bulk updated
$DateString = get-date -format "dd-MM-yyyy"
clear-host
Function Add-ADUser2TD {
<#
.SYNOPSIS
When Passed an AD User Object this Function will Determine if the user needs to be added or updated in Team Dynamix
It will then pass the info to the correct function based on Usertype.
.DESCRIPTION
Long description
.PARAMETER ADUser
A User Object from AD with all Properties exposed or at a minimum the following Properties:
-Department
-EmployeeNumber
-ExtensionAttribute1
-Office
-OfficePhone
-OtherMailbox
.EXAMPLE
$AdUser = Get-aduser -identity RRabbit -properties *
Add-ADUser2TD -Aduser $Aduser
.NOTES
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
In Authors environment Extension attribute1 should contain a value with the user type (employee, student, etc.)
This is Unique to this Institution: you may want to omit Or Severely Alter this Function code
#>
Param(
[Parameter(mandatory = $true)]
$ADUser
)
[string]$Username = $Aduser.SamAccountName
[string]$Email = $AdUser.UserPrincipalName
[string]$DN = $ADUser.DistinguishedName
[string]$AlternateEmail = $AdUser.OtherMailbox
[string]$FirstName = $AdUser.GivenName
[string]$LastName = $Aduser.Surname
[string]$Title = $AdUser.Title
[string]$Department = $AdUser.Department
[string]$EmployeeNumber = $AdUser.EmployeeNumber
[string]$UserType = $Aduser.ExtensionAttribute1
Write-host -ForegroundColor green "$Username : Usertype is $usertype"
If (!($UserType)){
write-host -ForegroundColor green "$username : Usertype is Null"
If (($DN -like "*OU=student*") -or ($DN -like "*OU=Alumni*")){
Write-host -ForegroundColor yellow "$Username : DN like student or alumni: $DN"
$UserType = 'student'
}
}
If ($UserType -ne 'student') {
$OfficePhone = $Aduser.Office
If ($null -eq $OfficePhone) { $OfficePhone = '0' }
If (($null -eq $Department) -or ($Department -eq '')) {
$Department = 'Other'
Write-host "Department was Null changed to Other"}
$WorkAddress = $AdUser.Office
}
else {
$OfficePhone = '0'
$WorkAddress = $Null
}
$Usercheck = Get-TDPersonByUsername -Username $Username #Verify if TD user Exists for this AD Account
$d = get-date
If ($UserCheck) {
If ($UserType -eq 'student') {
write-host "$d Running Function: Set-TDStudent to Update User $Username"
Write-Output "$d Running Function: Set-TDStudent to Update User $Username" | Out-File -FilePath $LogFile -Append
$NewTDUser = Set-TDStudent -TDUser $UserCheck -Username $Username -FirstName $FirstName -LastName $LastName -email $Email -AlternateEmail $AlternateEmail -EmployeeNumber $EmployeeNumber
}
Else {
Write-host "$d Running Function: Set-TDEmployee to Update User $Username"
Write-Output "$d Running Function: Set-TDEmployee to Update User $Username" | Out-File -FilePath $LogFile -Append
$NewTDUser = Set-TDEmployee -TDuser $Usercheck -Username $Username -FirstName $FirstName -LastName $LastName -email $Email -AlternateEmail $AlternateEmail -OfficePhone $OfficePhone -WorkAddress $WorkAddress -EmployeeNumber $EmployeeNumber -Title $Title -Department $Department
}
}
Else {
If ($UserType -eq 'student') {
write-host "$d Running Function: Add-TDStudent to Create User $Username"
Write-Output "$d Running Function: Add-TDStudent to Create User $Username" | Out-File -FilePath $LogFile -Append
$NewTDUser = Add-TDStudent -Username $Username -FirstName $FirstName -LastName $LastName -email $Email -AlternateEmail $AlternateEmail -EmployeeNumber $EmployeeNumber
}
Else {
write-host "$d Running Function: Add-TDEmployee to Create User $Username"
Write-Output "$d Running Function: Add-TDEmployee to Create User $Username" | Out-File -FilePath $LogFile -Append
$NewTDUser = Add-TDEmployee -Username $Username -FirstName $FirstName -LastName $LastName -email $Email -AlternateEmail $AlternateEmail -OfficePhone $OfficePhone -WorkAddress $WorkAddress -EmployeeNumber $EmployeeNumber -Title $Title -Department $Department
}
}
Return $NewTDUser
}
Function Add-TDDesktop {
<#
.SYNOPSIS
When Passed the Guid ID of a Desktop template in TD along with a TD Person's UID This function can add the desktop template to the user in TD
.DESCRIPTION
Long description
.PARAMETER DesktopID
Guid of the Desktop Template in TD. This can be extracted from the end of the URL string when looking at the Desktop in TD's Administration Page
For Now Acadia Is only interested in the "Desktop-2020" Template
.PARAMETER UserUids
An Array of UUID Strings Collected by all operations Adding or updating users
.PARAMETER isDefault
Boolean value of $true or $false to determine if the Desktop Template will be applied as the default desktop for this Person in TDClient
.EXAMPLE
Add-TDDesktop -DesktopID "106cb4a4-9d16-4f7e-859f-4f2f897ef8a2" -UserUid "c7d1e2d4-0efb-eb11-b831-005056a06586" -isDefault $True
.NOTES
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param (
[parameter(mandatory = $true)]
[string]$DesktopID,
[parameter(mandatory = $true)]
[Array]$UserUids,
[bool]$isDefault = $false
)
If ($IsDefault) {
$IsDefault = 'true'
}
else { $IsDefault = 'false' }
$body = $UserUids | ConvertTo-Json
Write-Host -ForegroundColor Yellow $body
$DesktopUri = "$tdURL/people/bulk/applydesktop/$DesktopID" + "?IsDefault=$IsDefault"
Write-Host -ForegroundColor green $DesktopURI
Invoke-RestMethod -Uri $DesktopUri -ContentType $contentType -Method POST -UseBasicParsing -Body $body -Headers $Global:headers
}
Function Get-TDUserGroupMembership {
param(
[parameter(mandatory=$true)]
$TDUserUID
)
#https://app.teamdynamix.com/TDWebApi/api/groups/{id}/members
$Uri = "$tdURL/people/$TDUserUID/groups"
$Groups = Invoke-RestMethod -Uri $Uri -ContentType $contentType -Method GET -UseBasicParsing -Headers $Global:headers
#Sleep Due to API Rate Limiting
start-sleep -Seconds 1
return $Groups
}
Function Remove-TDGroupMember {
<#
.SYNOPSIS
Add a Team Dynamix Person record to a Group. Optionaly this Group can be made the persons primary group.
.PARAMETER GroupID
A Numeric ID Number Associated with the Group. Can be viewed in TD when looking at the group in the Admin interface
.PARAMETER MemberUid
The UID of a Person record in Team Dynamix that will be added to the Group
.PARAMETER IsPrimary
Boolean value of $true or $false that will indicate weather the group should become the users Primary Group
.EXAMPLE
Add-TDGroupMember -GroupID "13" -MemberUid "c7d1e2d4-0efb-eb11-b831-005056a06586" -IsPrimary $true
.NOTES
For Now Acadia is only interested with two groups (Students[12], and Employees[13]) So we have statically used these values
In the future a another function to lookup group by name and extract the ID would be a good idea.
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
[parameter(mandatory = $true)]
[string]$GroupID,
[parameter(mandatory = $true)]
[string]$MemberUid
)
$body = "[`"$MemberUid`"]"
Write-Host -ForegroundColor Yellow $body
$FeedUri = "$tdURL/groups/$GroupID/Members"
Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method DELETE -UseBasicParsing -Body $body -Headers $Global:headers
}
Function Add-TDGroupMember {
<#
.SYNOPSIS
Add a Team Dynamix Person record to a Group. Optionaly this Group can be made the persons primary group.
.PARAMETER GroupID
A Numeric ID Number Associated with the Group. Can be viewed in TD when looking at the group in the Admin interface
.PARAMETER MemberUid
The UID of a Person record in Team Dynamix that will be added to the Group
.PARAMETER IsPrimary
Boolean value of $true or $false that will indicate weather the group should become the users Primary Group
.EXAMPLE
Add-TDGroupMember -GroupID "13" -MemberUid "c7d1e2d4-0efb-eb11-b831-005056a06586" -IsPrimary $true
.NOTES
For Now Acadia is only interested with two groups (Students[12], and Employees[13]) So we have statically used these values
In the future a another function to lookup group by name and extract the ID would be a good idea.
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
[parameter(mandatory = $true)]
[string]$GroupID,
[parameter(mandatory = $true)]
[string]$MemberUid,
[bool]$IsPrimary = $false
)
If ($ISPrimary) {
$Primary = 'true'
Remove-TDGroupMember -groupid $GroupID -MemberUid $MemberUid
}
else { $Primary = 'false' }
$body = "[`"$MemberUid`"]"
Write-Host -ForegroundColor Yellow $body
$FeedUri = "$tdURL/groups/$GroupID/Members?isPrimary=$Primary"
Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $body -Headers $Global:headers
}
Function Add-TDStudent {
<#
.SYNOPSIS
Add A New Student Person to Team Dynamix, Set Students as their Primary Group and set "Desktop-2020" ad default desktop template.
.PARAMETER Username
AD Username - String
.PARAMETER Email
Users Internal Eamil Address
.PARAMETER AlternateEmail
Users External or Alternate Email Address
.PARAMETER FirstName
Users First Name
.PARAMETER LastName
Users Last Name
.PARAMETER EmployeeNumber
Users Employee Number from AD (Colleague Person ID)
.EXAMPLE
Add-TDStudent -Username "jdoe" -Email "5555555d@acmeinc.ca" -AlternateEmail "FuzzyFuzzyKitten@gmail.com" -FirstName 'John' -LastName 'Doe' -EmployeeNumber "5555555"
.NOTES
Typically Called from the Add-ADUser2TD function
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
$Username,
$Email,
$AlternateEmail,
$FirstName,
$LastName,
$EmployeeNumber
)
#Append Username to Lastname so that Users can be searched by username in teamdynamix
$LastName = "$LastName ($UserName)"
#Static Info Unique to Your Institution
$Company = 'ACME Inc.'
$Title = 'Student'
$IsEmployee = $false
$WorkPhone = '0'
$WorkZip = '90210'
$DefaultAccountID = '77' #All Students are in the Student Account #Unique to Your Institution
$SecurityRoleID = '74bbbbc-1220-ffff-aaaa-977770d5b321' #Basic User Role ID Unique to Your Institution
$TDUser = New-Object -TypeName PSObject
$TDUser | Add-Member -Type NoteProperty -Name 'Username' -Value $Username
$TDUser | Add-Member -Type NoteProperty -Name 'FullName' -Value "$FirstName $LastName"
$TDUser | Add-Member -Type NoteProperty -Name 'FirstName' -Value $FirstName
$TDUser | Add-Member -Type NoteProperty -Name 'LastName' -Value $LastName
$TDUser | Add-Member -Type NoteProperty -Name 'PrimaryEmail' -Value $Email
$TDUser | Add-Member -Type NoteProperty -Name 'DefaultAccountID' -Value $DefaultAccountID
$TDUser | Add-Member -Type NoteProperty -Name 'SecurityRoleID' -Value $SecurityRoleID
$TDUser | Add-Member -Type NoteProperty -Name 'AlternateEmail' -Value $AlternateEmail
$TDUser | Add-Member -Type NoteProperty -Name 'ExternalID' -Value $EmployeeNumber
$TDUser | Add-Member -Type NoteProperty -Name 'AlternateID' -Value $EmployeeNumber
$TDUser | Add-Member -Type NoteProperty -Name 'AlertEmail' -Value $Email
$TDUser | Add-Member -Type NoteProperty -Name 'Company' -Value $Company
$TDUser | Add-Member -Type NoteProperty -Name 'Title' -Value $Title
$TDUser | Add-Member -Type NoteProperty -Name 'WorkPhone' -Value $WorkPhone
$TDUser | Add-Member -Type NoteProperty -Name 'WorkZip' -Value $WorkZip
$TDUser | Add-Member -Type NoteProperty -Name 'IsEmployee' -Value $IsEmployee
$TDUser | Add-Member -Type NoteProperty -Name 'AuthenticationUserName' -Value $Username
$TDUser | Add-Member -Type NoteProperty -Name 'TypeID' -Value '1'
$UJson = $TDUser | ConvertTo-Json
Write-Host $TDUser
Write-Host $UJson
$FeedUri = "$tdURL/people"
$CreationResponse = Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $UJson -Headers $Global:headers
Start-Sleep -Seconds 1
$NewUID = $CreationResponse.UID
Write-Host -ForegroundColor yellow "The New UID is: $NewUID"
$StudentGroupID = '99' #Unique to Your Institution
$d = get-date
Write-Output "$d Running Function: Add-ADGroupMember Adding $NewUID to Group $StudentGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $StudentGroupID -MemberUid $NewUID -IsPrimary $True
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$Global:UUIDs += $NewID
}
Function Add-TDEmployee {
<#
.SYNOPSIS
Add A New Employee Person to Team Dynamix, Set Employees as their Primary Group and set "Desktop-2020" ad default desktop template.
.PARAMETER Username
AD Username
.PARAMETER Email
Users Internal Eamil Address
.PARAMETER AlternateEmail
Users External or Alternate Email Address
.PARAMETER FirstName
Users First Name
.PARAMETER LastName
Users Last Name
.PARAMETER EmployeeNumber
Users Employee Number from AD (Colleague Person ID)
.PARAMETER OfficePhone
Office Phone number for the Employee
.PARAMETER WorkAddress
Typically the Building Code and office room number for the Employee
.EXAMPLE
Add-TDEmployee -Username "jdoe" -Email "john.doe@acmeinc.ca" -AlternateEmail "FuzzyFuzzyKitten@gmail.com" -FirstName 'John' -LastName 'Doe' -EmployeeNumber "5555555" -OfficePhone "(902)-585-1697" -WorkAddress "UHall 164"
.NOTES
Typically Called from the Add-ADUser2TD function
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
$Username,
$Email,
$AlternateEmail,
$FirstName,
$LastName,
$Title,
$Department,
$EmployeeNumber,
$OfficePhone,
$WorkAddress,
$headers
)
$LastName = "$LastName ($UserName)"
$Company = 'ACME Inc.'
$IsEmployee = $true
#$WorkPhone = "0"
$WorkZip = '90210'
$SecurityRoleID = '745ee2dc-1910-ffff-aaaa-977770d5b321' #Basic User Role ID
$DefaultAccountID = Get-TDAccountID -AccountName $Department
$EmployeeGroupID = '88' #Group ID for Employees from TD
$TDUser = New-Object -TypeName PSObject
$TDUser | Add-Member -Type NoteProperty -Name 'Username' -Value $Username
$TDUser | Add-Member -Type NoteProperty -Name 'FullName' -Value "$FirstName $LastName"
$TDUser | Add-Member -Type NoteProperty -Name 'FirstName' -Value $FirstName
$TDUser | Add-Member -Type NoteProperty -Name 'LastName' -Value $LastName
$TDUser | Add-Member -Type NoteProperty -Name 'PrimaryEmail' -Value $Email
$TDUser | Add-Member -Type NoteProperty -Name 'DefaultAccountID' -Value $DefaultAccountID
$TDUser | Add-Member -Type NoteProperty -Name 'SecurityRoleID' -Value $SecurityRoleID
$TDUser | Add-Member -Type NoteProperty -Name 'AlternateEmail' -Value $AlternateEmail
$TDUser | Add-Member -Type NoteProperty -Name 'ExternalID' -Value $EmployeeNumber
$TDUser | Add-Member -Type NoteProperty -Name 'AlternateID' -Value $EmployeeNumber
$TDUser | Add-Member -Type NoteProperty -Name 'AlertEmail' -Value $Email
$TDUser | Add-Member -Type NoteProperty -Name 'Company' -Value $Company
$TDUser | Add-Member -Type NoteProperty -Name 'Title' -Value $Title
$TDUser | Add-Member -Type NoteProperty -Name 'WorkPhone' -Value $OfficePhone
$TDUser | Add-Member -Type NoteProperty -Name 'WorkAddress' -Value $WorkAddress
$TDUser | Add-Member -Type NoteProperty -Name 'WorkZip' -Value $WorkZip
$TDUser | Add-Member -Type NoteProperty -Name 'IsEmployee' -Value $IsEmployee
$TDUser | Add-Member -Type NoteProperty -Name 'AuthenticationUserName' -Value $Username
$TDUser | Add-Member -Type NoteProperty -Name 'TypeID' -Value '1'
$UJson = $TDUser | ConvertTo-Json
Write-Host $TDUser
Write-Host $UJson
$FeedUri = "$tdURL/people"
$CreationResponse = Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $UJson -Headers $Global:headers
Start-Sleep -Seconds 1
$NewUID = $CreationResponse.UID
Write-Host -ForegroundColor yellow "The User UID is: $NewUID"
#Add User to Employees Group
$d = get-date
Write-Output "$d Running Function: Add-TDGroupMember Adding $NewUID to Group $EmployeeGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $EmployeeGroupID -MemberUid $NewUID -IsPrimary $True
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$Global:UUIDs += $NewID
}
Function Set-TDStudent {
<#
.SYNOPSIS
Updates an existing Student Person In Team Dynamix, Set Students as their Primary Group and set "Desktop-2020" ad default desktop template.
.PARAMETER TDUser
A Powershell object return by Get-TDPersonByUsername
Will be updated, converted to json and then posted back to Team Dynamix
.PARAMETER Username
AD Username - String
.PARAMETER Email
Users Internal Eamil Address
.PARAMETER AlternateEmail
Users External or Alternate Email Address
.PARAMETER FirstName
Users First Name
.PARAMETER LastName
Users Last Name
.PARAMETER EmployeeNumber
Users Employee Number from AD (Colleague Person ID)
.EXAMPLE
$AdUser = Get-TDPersonByUsername -Username 'jdoe'
Set-TDStudent -TDUser $TDuser -Username "jdoe" -Email "5555555d@acmeinc.ca" -AlternateEmail "FuzzyFuzzyKitten@gmail.com" -FirstName 'John' -LastName 'Doe' -EmployeeNumber "5555555"
.NOTES
Typically Called from the Add-ADUser2TD function
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
$TDUser,
$Username,
$Email,
$AlternateEmail,
$FirstName,
$LastName,
$EmployeeNumber,
$headers
)
#Append Username to Lastname so that Users can be searched by username in teamdynamix
$LastName = "$LastName ($UserName)"
#Static Info Unique to Your Institution
$Company = 'ACME Inc.'
$Title = 'Student'
$IsEmployee = $false
$WorkPhone = '0'
$WorkZip = '90210'
$DefaultAccountID = '77' #All Students are in the Student Account
$TDUserUID = $TDUser.UID
$TDUser.Username = $Username
$TDUser.FullName = "$FirstName $LastName"
$TDUser.FirstName = $FirstName
$TDUser.LastName = $LastName
$TDUser.PrimaryEmail = $Email
$TDUser.DefaultAccountID = $DefaultAccountID
$TDUser.AlternateEmail = $AlternateEmail
$TDUser.ExternalID = $EmployeeNumber
$TDUser.AlternateID = $EmployeeNumber
$TDUser.AlertEmail = $Email
$TDUser.Company = $Company
$TDUser.Title = $Title
$TDUser.WorkPhone = $WorkPhone
$TDUser.WorkZip = $WorkZip
$TDUser.IsEmployee = $IsEmployee
$TDUser.AuthenticationUserName = $Username
$TDUser.TypeID = '1'
$UJson = $TDUser | ConvertTo-Json
#Write-Host $TDUser
#Write-Host $UJson
$FeedUri = "$tdURL/people/$TDUserUID"
$CreationResponse = Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $UJson -Headers $Global:headers
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$NewUID = $CreationResponse.UID
Write-Host -ForegroundColor yellow "The User UID is: $NewUID"
$StudentGroupID = '99' #Unique to Your Institution
$d = get-date
#Examine Existing TD Group Memberships
$TDGroups = Get-TDUserGroupMembership -TDUserUID $TDUserUID
$PrimaryTDGroup = $TDGroups | Where-object {$_.isPrimary -eq 'True'} # Check to validate f the Student Group is alreay primary
If (($TDGroups.GroupID -Contains $StudentGroupID) -And ($PrimaryTDGroup.GroupID -eq $StudentGroupID)){
Write-Output "$d $Username ($NewUID) Already a Member of Group $StudentGroupID as Primary" | Out-File -FilePath $LogFile -Append
}
ElseIF (($TDGroups.GroupID -Contains $StudentGroupID) -and ($PrimaryTDGroup.GroupID -ne $StudentGroupID) -And ($PrimaryTDGroup)){
Write-Output "$d $Username ($NewUID) Already a Member of Group $StudentGroupID NOT Primary -Leaving Unchanged" | Out-File -FilePath $LogFile -Append
}
Elseif (!($PrimaryTDGroup) -And ($TDGroups.GroupID -Contains $StudentGroupID)){
#Remove then add as primary
Write-Output "$d Running Function: Remove-TDGroupMember Remove $NewUID From Group $StudentGroupID So They can be re-added as Primary" | Out-File -FilePath $LogFile -Append
Remove-TDGroupMember -GroupID $StudentGroupID -MemberUid $NewUID
Write-Output "$d Running Function: Add-TDGroupMember Adding $NewUID to Group $StudentGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $StudentGroupID -MemberUid $NewUID -IsPrimary $True
}
Elseif (!($PrimaryTDGroup) -And ($TDGroups.GroupID -NotContains $StudentGroupID)){
#Add as Primary
Write-Output "$d Running Function: Add-TDGroupMember Adding $NewUID to Group $StudentGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $StudentGroupID -MemberUid $NewUID -IsPrimary $True
}
Elseif(($PrimaryTDGroup) -And ($TDGroups.GroupID -NotContains $StudentGroupID)){
#Add Not as Primary
Write-Output "$d Running Function: Add-TDGroupMember Adding $NewUID to Group $StudentGroupID But Leaving Existing Primary Group Unchanged" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $StudentGroupID -MemberUid $NewUID -IsPrimary $False
}
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$Global:UUIDs += $NewID
}
Function Set-TDEmployee {
<#
.SYNOPSIS
Updates an existing Employee Person to Team Dynamix, Set Employees as their Primary Group and set "Desktop-2020" ad default desktop template.
.PARAMETER TDUser
A Powershell object return by Get-TDPersonByUsername
Will be updated, converted to json and then posted back to Team Dynamix
.PARAMETER Username
AD Username
.PARAMETER Email
Users Internal Eamil Address
.PARAMETER AlternateEmail
Users External or Alternate Email Address
.PARAMETER FirstName
Users First Name
.PARAMETER LastName
Users Last Name
.PARAMETER EmployeeNumber
Users Employee Number from AD (Colleague Person ID)
.PARAMETER OfficePhone
Office Phone number for the Employee
.PARAMETER WorkAddress
Typically the Building Code and office room number for the Employee
.EXAMPLE
$TDUser = Get-TDPersonByUsername -username 'jdoe'
Add-TDEmployee -Username "jdoe" -Email "john.doe@acmeinc.ca" -AlternateEmail "FuzzyFuzzyKitten@gmail.com" -FirstName 'John' -LastName 'Doe' -EmployeeNumber "5555555" -OfficePhone "(902)-585-1697" -WorkAddress "UHall 164"
.NOTES
Typically Called from the Add-ADUser2TD function
Requires a Global Variable $headers which contains a bearer token authorizing access to the TD API
ex: $Global:headers = @{Authorization = "Bearer $bearer_token"}
#>
Param(
$TDUser,
$Username,
$Email,
$AlternateEmail,
$FirstName,
$LastName,
$Title,
$Department,
$EmployeeNumber,
$OfficePhone,
$WorkAddress,
$headers
)
$LastName = "$LastName ($UserName)"
$Company = 'ACME Inc.'
$IsEmployee = $true
#$WorkPhone = "0"
$WorkZip = '90210'
$DefaultAccountID = Get-TDAccountID -AccountName $Department
$EmployeeGroupID = '88' #Unique to Your Institution
$TDUserUID = $TDUser.UID
#$TDUser = New-Object -TypeName PSObject
$TDUser.Username = $Username
$TDUser.FullName = "$FirstName $LastName"
$TDUser.FirstName = $FirstName
$TDUser.LastName = $LastName
$TDUser.PrimaryEmail = $Email
$TDUser.DefaultAccountID = $DefaultAccountID
$TDUser.AlternateEmail = $AlternateEmail
$TDUser.ExternalID = $EmployeeNumber
$TDUser.AlternateID = $EmployeeNumber
$TDUser.AlertEmail = $Email
$TDUser.Company = $Company
$TDUser.Title = $Title
$TDUser.WorkPhone = $OfficePhone
$TDUser.WorkAddress = $WorkAddress
$TDUser.WorkZip = $WorkZip
$TDUser.IsEmployee = $IsEmployee
$TDUser.AuthenticationUserName = $Username
$TDUser.TypeID = '1'
$UJson = $TDUser | ConvertTo-Json
Write-Host $TDUser
Write-Host $UJson
$FeedUri = "$tdURL/people/$TDUserUID"
$CreationResponse = Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $UJson -Headers $Global:headers
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$NewUID = $CreationResponse.UID
Write-Host -ForegroundColor yellow "The New UID is: $NewUID"
#Add User to Employees Group
$d = get-date
#Examine Existing TD Group Memberships
$TDGroups = Get-TDUserGroupMembership -TDUserUID $TDUserUID
$PrimaryTDGroup = $TDGroups | Where-object {$_.isPrimary -eq 'True'}
If (($TDGroups.GroupID -Contains $EmployeeGroupID) -And ($PrimaryTDGroup.GroupID -eq $EmployeeGroupID)){
Write-Output "$d $Username ($NewUID) Already a Member of Group $EmployeeGroupID as Primary" | Out-File -FilePath $LogFile -Append
}
ElseIF (($TDGroups.GroupID -Contains $EmployeeGroupID) -and ($PrimaryTDGroup.GroupID -ne $EmployeeGroupID) -And ($PrimaryTDGroup)){
Write-Output "$d $Username ($TDUserUID) Already a Member of Group $EmployeeGroupID NOT Primary -Leaving Unchanged" | Out-File -FilePath $LogFile -Append
}
Elseif (!($PrimaryTDGroup) -And ($TDGroups.GroupID -Contains $EmployeeGroupID)){
#Remove then add as primary
Write-Output "$d Running Function: Remove-TDGroupMember Remove $TDUserUID From Group $EmployeeGroupID So They can be re-added as Primary" | Out-File -FilePath $LogFile -Append
Remove-TDGroupMember -GroupID $EmployeeGroupID -MemberUid $TDUserUID
Write-Output "$d Running Function: Add-TDGroupMember Adding $TDUserUID to Group $EmployeeGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $EmployeeGroupID -MemberUid $TDUserUID -IsPrimary $True
}
Elseif (!($PrimaryTDGroup) -And ($TDGroups.GroupID -NotContains $EmployeeGroupID)){
#Add as Primary
Write-Output "$d Running Function: Add-TDGroupMember Adding $TDUserUID to Group $EmployeeGroupID as Primary" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $EmployeeGroupID -MemberUid $TDUserUID -IsPrimary $True
}
Elseif(($PrimaryTDGroup) -And ($TDGroups.GroupID -NotContains $EmployeeGroupID)){
#Add Not as Primary
Write-Output "$d Running Function: Add-TDGroupMember Adding $TDUserUID to Group $EmployeeGroupID But Leaving Existing Primary Group Unchanged" | Out-File -FilePath $LogFile -Append
Add-TDGroupMember -GroupID $EmployeeGroupID -MemberUid $TDUserUID -IsPrimary $False
}
#Sleep Due to API Rate Limiting
Start-Sleep -Seconds 1
$Global:UUIDs += $NewID
}
##
Function Get-AdminBearer {
<#
.SYNOPSIS
Authenticates with Admin API endpoint and returns Bearer token good for 24 hours. Uses Global Variables to populate BEID and WebServicesKey
.EXAMPLE
$AdminBearer = Get-AdminBearer
.NOTES
This is Required when working with People etc from the API a normal service user Bearer Token will not provide
sufficient access.
#>
$url = $tdURL
$action = '/auth/loginadmin'
$endpoint = $url + $action
$request = "{
`"BEID`": `"$Global:BEID`",
`"WebServicesKey`": `"$Global:WebServicesID`"
}"
$Bearer = Invoke-RestMethod -Method Post -Uri $endpoint -Body $request -ContentType 'application/json; charset=utf-8'
return $Bearer
}
Function Get-TDAccountID {
<#
.SYNOPSIS
Returns the Guid ID of a Department/Account in Team Dynamix from the Department/Account Name passed as AccountName parameter
.PARAMETER AccountName
String Value Department Name
.EXAMPLE
Get-TDAccountID -AccountName "Technology Services"
.NOTES
Used by add-TDEmployee and Set-TDEmployee to add the default acct/dept based on the department field in AD
#>
param (
[parameter(mandatory=$true)]
$AccountName
)
$AccountSearchURI = $tdURL + '/accounts/search'
$body = New-Object -TypeName PSObject
$body | Add-Member -Type NoteProperty -Name 'SearchText' -Value $AccountName
$jbody = $body | ConvertTo-Json
Write-Host $body
Write-Host -ForegroundColor yellow $jbody
$Account = Invoke-RestMethod -Uri $AccountSearchURI -ContentType $contentType -Method POST -UseBasicParsing -Body $jbody -Headers $Global:headers
return $Account.ID
}
Function Get-TDPersonByUsername {
<#
.SYNOPSIS
Returns a User Object from TD Based on the Username passed as a parameter
.PARAMETER Username
Username of the person object being looked update
.EXAMPLE
Get-TDPersonByUsername -Username 'RRabbit'
.NOTES
Used by Add-ADUser2TD function to determine if user already exists and to pass the user object to Set-TDEmployee or Set-TDStudent
#>
param(
[parameter(Mandatory=$true)]
[string] $Username
)
$Update = "{`"username`": `"$Username`"}"
$FeedUri = "$tdURL/people/search"
$SearchResponse = Invoke-RestMethod -Uri $FeedUri -ContentType $contentType -Method POST -UseBasicParsing -Body $Update -Headers $Global:headers
# Inject sleep statement to prevent API throttling
start-sleep -Milliseconds 500
# Return Full User Attributes: https://app.teamdynamix.com/TDWebApi/api/people/{uid}
If ($SearchResponse){
#Search returns limited attributes Getting users full attribute set and returning it.
[string]$Uid = $SearchResponse.uid
Write-host -ForegroundColor Yellow "Collecting Full Attributes from TD for User ID: $Uid"
$PeopleURI = "$tdURL/people/$Uid"
$fullUser = Invoke-RestMethod -Uri $PeopleUri -ContentType $contentType -Method GET -UseBasicParsing -Headers $Global:headers
# Inject sleep statement to prevent API throttling
start-sleep -Milliseconds 500
Return $fullUser
}
}
Function Get-BearerToken {
<#
.SYNOPSIS
Authenticates a user account and returns a bearer token to be used with the API representing that user
.PARAMETER User
String Username
.PARAMETER Password
String Password
.EXAMPLE
get-bearertoken -username "ServiceAcct" -Password "SuperSecretPassword"
.NOTES
Can not be used with all admin functions such as Managing people.
#>
param(
[parameter(mandatory=$true)]
[String] $User,
[parameter(mandatory=$true)]
[string] $Password
)
#Get Credentials if not passed as params
If (($null -eq $User) -Or ($null -eq $Password)) {
$credin = Get-Credential
$User = $credin.UserName
$Password = $credin.GetNetworkCredential().password
}
#Json Body to pass to API to obtain bearer Token
$body = "{username: '$User', password: '$Password'}"
$TokenRequest = Invoke-RestMethod -UseBasicParsing -Uri "$tdURL/auth" -ContentType $contentType -Method POST -Body $body -Headers $Global:headers
return $TokenRequest
}
#Add the following to Script to get bearer token for API integration.. Working with some aspects like people require an admin bearer_token
#$bearer_token = Get-BearerToken -User "$un" -Password $pw
$bearer_token = Get-AdminBearer
#Generate headers with Bearer Token to be used in all API related functions
$Global:headers = @{Authorization = "Bearer $bearer_token" }