Skip to content

Commit

Permalink
Fixed Duplicate Relations
Browse files Browse the repository at this point in the history
  • Loading branch information
lwhitelock committed Sep 19, 2023
1 parent 106110f commit bb9844d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Modules/CippExtensions/NinjaOne/Invoke-NinjaOneTenantSync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ function Invoke-NinjaOneTenantSync {

[System.Collections.Generic.List[PSCustomObject]]$NinjaOneUserDocs = $NinjaOneOrgDocs | Where-Object { $_.documentTemplateId -eq $NinjaOneUsersTemplate.id }

# Get NinjaOne Related Items
$RelatedItems = (Invoke-WebRequest -uri "https://$($Configuration.Instance)/api/v2/related-items" -Method GET -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json').content | ConvertFrom-Json -depth 100

# Create the update objects we will use to update NinjaOne
$NinjaOrgUpdate = [PSCustomObject]@{}
[System.Collections.Generic.List[PSCustomObject]]$NinjaUserUpdates = @()
Expand Down Expand Up @@ -719,7 +722,7 @@ function Invoke-NinjaOneTenantSync {
}
}

if ($MappedFields.DeviceCompliance){
if ($MappedFields.DeviceCompliance) {
$NinjaDeviceUpdate | Add-Member -NotePropertyName $MappedFields.DeviceCompliance -NotePropertyValue $Device.complianceState
}

Expand Down Expand Up @@ -1173,16 +1176,20 @@ function Invoke-NinjaOneTenantSync {

# Relate Users to Devices
Foreach ($LinkDevice in $ParsedDevices | Where-Object { $null -ne $_.NinjaDevice }) {
$RelatedItems = (Invoke-WebRequest -uri "https://$($Configuration.Instance)/api/v2/related-items/with-entity/NODE/$($LinkDevice.NinjaDevice.id)" -Method GET -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json').content | ConvertFrom-Json -depth 100
[System.Collections.Generic.List[PSCustomObject]]$Relations = @()
Foreach ($LinkUser in $LinkDevice.UserIDs) {
$MatchedUser = $UsersMap | Where-Object { $_.M365ID -eq $LinkUser }
if (($MatchedUser | Measure-Object).count -eq 1) {
$Relations.Add(
[PSCustomObject]@{
relEntityType = "DOCUMENT"
relEntityId = $MatchedUser.NinjaOneID
}
)
$ExistingRelation = $RelatedItems | Where-Object { $_.relEntityType -eq 'DOCUMENT' -and $_.relEntityId -eq $MatchedUser.NinjaOneID }
if (!$ExistingRelation) {
$Relations.Add(
[PSCustomObject]@{
relEntityType = "DOCUMENT"
relEntityId = $MatchedUser.NinjaOneID
}
)
}
}
}

Expand Down

0 comments on commit bb9844d

Please sign in to comment.