-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphAPI_CreateContact_SharepointList.ps1
107 lines (90 loc) · 3.43 KB
/
GraphAPI_CreateContact_SharepointList.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
#Connect-PnPOnline -url ""
#custom compare function Compare-Object only returned null even with properties
function CompareContacts($existCont,$newCont){
$properties = @(
"givenName","surname","emailAddresses","businessPhone","categories","CompanyName","Department","OfficeLocation"
)
foreach($property in $properties){
$existVal = $existCont.$property
$newVal = $newCont.$property
if($existVal -is [System.Collections.IEnumerable] -and $newVal -is [System.Collections.IEnumerable]){
$existVal = @($existVal) -join ","
$newVal = @($newVal) -join ","
}
if($existVal -ne $newVal){
return $true
}
}
return $false
}
# Benchmark start
$benchmark = [System.Diagnostics.Stopwatch]::StartNew()
$userUPN = ""
$items = Get-PnPListItem -list ""
if($items.count -eq 0){
Write-Host "Items werent retrieved or list is emtpy"
return
}
$itemsToExport = $items | ForEach-Object {
[PSCustomObject]@{
GivenName = $_.FieldValues.Title
Surname = $_.FieldValues.Surname
Initials = $_.FieldValues.Initials
emailAddresses = $_.FieldValues.emailAddresses
emailNames = $_.FieldValues.emailNames
businessPhones = $_.FieldValues.businessPhones
categories = $_.FieldValues.categories
CompanyName = $_.FieldValues.CompanyName
Department = $_.FieldValues.Department
OfficeLocation = $_.FieldValues.OfficeLocation
}
}
foreach ($contact in $itemsToExport) {
$params = @{
givenName = $contact.givenName
surname = $contact.surname
emailAddresses = @(
@{
address = $contact.emailAddresses
name = $contact.emailNames
}
)
businessPhones = @($contact.businessPhones)
categories = @($contact.categories)
CompanyName = $contact.CompanyName
Department = $contact.Department
OfficeLocation = $contact.OfficeLocation
}
$existingContact = Get-MgUserContact -UserId $userUPN -Filter "emailAddresses/any(a: a/address eq '$($contact.emailAddresses)')"
if($null -eq $existingContact){
New-MgUserContact -UserId $userUPN -BodyParameter $params
} else {
$oldParams = @{
givenName = $existingContact.givenName
surname = $existingContact.surname
emailAddresses = @(
@{
address = $existingContact.emailAddresses
name = $existingContact.emailNames
}
)
businessPhones = @($existingContact.businessPhones)
categories = @($existingContact.categories)
CompanyName = $existingContact.CompanyName
Department = $existingContact.Department
OfficeLocation = $existingContact.OfficeLocation
}
$differences = CompareContacts -existCont $oldParams -newCont $params
if($differences){
Remove-MgUserContact -UserId $userUPN -ContactId $existingContact.Id
New-MgUserContact -UserId $userUPN -BodyParameter $params
} else {
Write-Host "Contact '$($contact.givenName) $($contact.surname)' already exists, contact was skipped"
}
}
}
# Benchmark stop
$benchmark.Stop()
Write-Host "Benchmarking:`n-----------------------------"
Write-Host "Time elapsed: "$benchmark.Elapsed
Write-Host "Time elapsed (ms): "$benchmark.ElapsedMilliseconds"`n"