Skip to content

Commit da70c01

Browse files
committed
fix ninja logging
1 parent 62595cc commit da70c01

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

Modules/CippExtensions/Public/NinjaOne/Invoke-NinjaOneSync.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Invoke-NinjaOneSync {
3232

3333
Add-AzDataTableEntity @Table -Entity $AddObject -Force
3434

35-
Write-LogMessage -API 'NinjaOneAutoMap_Queue' -Headers'CIPP' -message "NinjaOne Synchronization Queued for $(($TenantsToProcess | Measure-Object).count) Tenants" -Sev 'Info'
35+
Write-LogMessage -API 'NinjaOneAutoMap_Queue' -Headers 'CIPP' -message "NinjaOne Synchronization Queued for $(($TenantsToProcess | Measure-Object).count) Tenants" -Sev 'Info'
3636
} catch {
3737
Write-LogMessage -API 'Scheduler_Billing' -tenant 'none' -message "Could not start NinjaOne Sync $($_.Exception.Message)" -sev Error
3838
}

Modules/CippExtensions/Public/NinjaOne/Invoke-NinjaOneTenantSync.ps1

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function Invoke-NinjaOneTenantSync {
2323
$EndDate = try { Get-Date($CurrentItem.lastEndTime) } catch { $Null }
2424

2525
if (($null -ne $CurrentItem.lastStartTime) -and ($StartDate -gt (Get-Date).ToUniversalTime().AddMinutes(-10)) -and ( $Null -eq $CurrentItem.lastEndTime -or ($StartDate -gt $EndDate))) {
26-
Throw "NinjaOne Sync for Tenant $($MappedTenant.RowKey) is still running, please wait 10 minutes and try again."
26+
throw "NinjaOne Sync for Tenant $($MappedTenant.RowKey) is still running, please wait 10 minutes and try again."
2727
}
2828

2929
# Set Last Start Time
@@ -45,10 +45,10 @@ function Invoke-NinjaOneTenantSync {
4545
$Customer = Get-Tenants -IncludeErrors | Where-Object { $_.customerId -eq $MappedTenant.RowKey }
4646
Write-Information "Processing: $($Customer.displayName) - Queued for $((New-TimeSpan -Start $StartQueueTime -End $StartTime).TotalSeconds)"
4747

48-
Write-LogMessage -API 'NinjaOneSync' -Headers'NinjaOneSync' -message "Processing NinjaOne Synchronization for $($Customer.displayName) - Queued for $((New-TimeSpan -Start $StartQueueTime -End $StartTime).TotalSeconds)" -Sev 'Info'
48+
Write-LogMessage -tenant $Customer.defaultDomainName -API 'NinjaOneSync' -message "Processing NinjaOne Synchronization for $($Customer.displayName) - Queued for $((New-TimeSpan -Start $StartQueueTime -End $StartTime).TotalSeconds)" -Sev 'Info'
4949

5050
if (($Customer | Measure-Object).count -ne 1) {
51-
Throw "Unable to match the recieved ID to a tenant QueueItem: $($QueueItem | ConvertTo-Json -Depth 100 | Out-String) Matched Customer: $($Customer| ConvertTo-Json -Depth 100 | Out-String)"
51+
throw "Unable to match the recieved ID to a tenant QueueItem: $($QueueItem | ConvertTo-Json -Depth 100 | Out-String) Matched Customer: $($Customer| ConvertTo-Json -Depth 100 | Out-String)"
5252
}
5353

5454
$TenantFilter = $Customer.defaultDomainName
@@ -303,7 +303,7 @@ function Invoke-NinjaOneTenantSync {
303303

304304
$MaxSecureScore = $CurrentSecureScore.maxScore
305305

306-
[System.Collections.Generic.List[PSCustomObject]]$SecureScoreParsed = Foreach ($Score in $CurrentSecureScore.controlScores) {
306+
[System.Collections.Generic.List[PSCustomObject]]$SecureScoreParsed = foreach ($Score in $CurrentSecureScore.controlScores) {
307307
$MatchedProfile = $SecureScoreProfiles | Where-Object { $_.id -eq $Score.controlName }
308308
[PSCustomObject]@{
309309
Category = $Score.controlCategory
@@ -448,7 +448,7 @@ function Invoke-NinjaOneTenantSync {
448448
}
449449

450450
# Parse Devices
451-
Foreach ($Device in $Devices | Where-Object { $_.id -notin $ParsedDevices.id }) {
451+
foreach ($Device in $Devices | Where-Object { $_.id -notin $ParsedDevices.id }) {
452452

453453
# First lets match on serial
454454
$MatchedNinjaDevice = $NinjaDevices | Where-Object { $_.system.biosSerialNumber -eq $Device.SerialNumber -or $_.system.serialNumber -eq $Device.SerialNumber }
@@ -489,7 +489,7 @@ function Invoke-NinjaOneTenantSync {
489489

490490

491491

492-
Foreach ($DeviceUser in $Device.usersloggedon) {
492+
foreach ($DeviceUser in $Device.usersloggedon) {
493493
$FoundUser = ($Users | Where-Object { $_.id -eq $DeviceUser.userid })
494494
$DeviceUsers.add($FoundUser.DisplayName)
495495
$DeviceUserIDs.add($DeviceUser.userId)
@@ -753,7 +753,7 @@ function Invoke-NinjaOneTenantSync {
753753

754754
$NinjaOneUser = $NinjaOneUserDocs | Where-Object { $_.ParsedFields.cippUserID -eq $User.ID }
755755
if (($NinjaOneUser | Measure-Object).count -gt 1) {
756-
Throw 'Multiple Users with the same ID found'
756+
throw 'Multiple Users with the same ID found'
757757
}
758758

759759

@@ -847,7 +847,7 @@ function Invoke-NinjaOneTenantSync {
847847
}
848848

849849
# OS Icon
850-
$OSIcon = Switch ($UserDevice.OS) {
850+
$OSIcon = switch ($UserDevice.OS) {
851851
'Windows' { '<i class="fab fa-windows"></i>' }
852852
'iOS' { '<i class="fab fa-apple"></i>' }
853853
'Android' { '<i class="fab fa-android"></i>' }
@@ -1180,7 +1180,7 @@ function Invoke-NinjaOneTenantSync {
11801180
Remove-AzDataTableEntity -Force @UsersUpdateTable -Entity $NinjaUserCreation
11811181
[System.Collections.Generic.List[PSCustomObject]]$NinjaUserCreation = @()
11821182
}
1183-
} Catch {
1183+
} catch {
11841184
Write-Information "Bulk Creation Error, but may have been successful as only 1 record with an issue could have been the cause: $_"
11851185
}
11861186

@@ -1192,7 +1192,7 @@ function Invoke-NinjaOneTenantSync {
11921192
Remove-AzDataTableEntity -Force @UsersUpdateTable -Entity $NinjaUserUpdates
11931193
[System.Collections.Generic.List[PSCustomObject]]$NinjaUserUpdates = @()
11941194
}
1195-
} Catch {
1195+
} catch {
11961196
Write-Information "Bulk Update Errored, but may have been successful as only 1 record with an issue could have been the cause: $_"
11971197
}
11981198

@@ -1255,7 +1255,7 @@ function Invoke-NinjaOneTenantSync {
12551255
Remove-AzDataTableEntity -Force @UsersUpdateTable -Entity $NinjaUserCreation
12561256

12571257
}
1258-
} Catch {
1258+
} catch {
12591259
Write-Information "Bulk Creation Error, but may have been successful as only 1 record with an issue could have been the cause: $_"
12601260
}
12611261

@@ -1266,7 +1266,7 @@ function Invoke-NinjaOneTenantSync {
12661266
[System.Collections.Generic.List[PSCustomObject]]$UpdatedUsers = (Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/organization/documents" -Method PATCH -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json; charset=utf-8' -Body ("[$($NinjaUserUpdates.body -join ',')]") -EA Stop).content | ConvertFrom-Json -Depth 100
12671267
Remove-AzDataTableEntity -Force @UsersUpdateTable -Entity $NinjaUserUpdates
12681268
}
1269-
} Catch {
1269+
} catch {
12701270
Write-Information "Bulk Update Errored, but may have been successful as only 1 record with an issue could have been the cause: $_"
12711271
}
12721272

@@ -1311,10 +1311,10 @@ function Invoke-NinjaOneTenantSync {
13111311

13121312

13131313
# Relate Users to Devices
1314-
Foreach ($LinkDevice in $ParsedDevices | Where-Object { $null -ne $_.NinjaDevice }) {
1314+
foreach ($LinkDevice in $ParsedDevices | Where-Object { $null -ne $_.NinjaDevice }) {
13151315
$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
13161316
[System.Collections.Generic.List[PSCustomObject]]$Relations = @()
1317-
Foreach ($LinkUser in $LinkDevice.UserIDs) {
1317+
foreach ($LinkUser in $LinkDevice.UserIDs) {
13181318
$MatchedUser = $UsersMap | Where-Object { $_.M365ID -eq $LinkUser }
13191319
if (($MatchedUser | Measure-Object).count -eq 1) {
13201320
$ExistingRelation = $RelatedItems | Where-Object { $_.relEntityType -eq 'DOCUMENT' -and $_.relEntityId -eq $MatchedUser.NinjaOneID }
@@ -1338,7 +1338,7 @@ function Invoke-NinjaOneTenantSync {
13381338
$Null = Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/related-items/entity/NODE/$($LinkDevice.NinjaDevice.id)/relations" -Method POST -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json' -Body ($Relations | ConvertTo-Json -Depth 100 -AsArray) -EA Stop
13391339
Write-Information 'Completed Update'
13401340
}
1341-
} Catch {
1341+
} catch {
13421342
Write-Information "Creating Relations Failed: $_"
13431343
}
13441344
}
@@ -1449,7 +1449,7 @@ function Invoke-NinjaOneTenantSync {
14491449
Write-Information 'Creating NinjaOne Licenses'
14501450
[System.Collections.Generic.List[PSCustomObject]]$CreatedLicenses = (Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/organization/documents" -Method POST -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json; charset=utf-8' -Body ($NinjaLicenseCreation | ConvertTo-Json -Depth 100 -AsArray) -EA Stop).content | ConvertFrom-Json -Depth 100
14511451
}
1452-
} Catch {
1452+
} catch {
14531453
Write-Information "Bulk Creation Error, but may have been successful as only 1 record with an issue could have been the cause: $_"
14541454
}
14551455

@@ -1460,21 +1460,21 @@ function Invoke-NinjaOneTenantSync {
14601460
[System.Collections.Generic.List[PSCustomObject]]$UpdatedLicenses = (Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/organization/documents" -Method PATCH -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json; charset=utf-8' -Body ($NinjaLicenseUpdates | ConvertTo-Json -Depth 100 -AsArray) -EA Stop).content | ConvertFrom-Json -Depth 100
14611461
Write-Information 'Completed Update'
14621462
}
1463-
} Catch {
1463+
} catch {
14641464
Write-Information "Bulk Update Errored, but may have been successful as only 1 record with an issue could have been the cause: $_"
14651465
}
14661466

14671467
[System.Collections.Generic.List[PSCustomObject]]$LicenseDocs = $CreatedLicenses + $UpdatedLicenses
14681468

14691469
if ($Configuration.LicenseDocumentsEnabled -eq $True -and $Configuration.UserDocumentsEnabled -eq $True) {
14701470
# Relate Subscriptions to Users
1471-
Foreach ($LinkLic in $LicenseDetails) {
1471+
foreach ($LinkLic in $LicenseDetails) {
14721472
$MatchedLicDoc = $LicenseDocs | Where-Object { $_.documentName -eq $LinkLic.name }
14731473
if (($MatchedLicDoc | Measure-Object).count -eq 1) {
14741474
# Remove existing relations
14751475
$RelatedItems = (Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/related-items/with-entity/DOCUMENT/$($MatchedLicDoc.documentId)" -Method GET -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json').content | ConvertFrom-Json -Depth 100
14761476
[System.Collections.Generic.List[PSCustomObject]]$Relations = @()
1477-
Foreach ($LinkUser in $LinkLic.Users) {
1477+
foreach ($LinkUser in $LinkLic.Users) {
14781478
$ExistingRelation = $RelatedItems | Where-Object { $_.relEntityType -eq 'DOCUMENT' -and $_.relEntityId -eq $LinkUser }
14791479
if (!$ExistingRelation) {
14801480
$Relations.Add(
@@ -1494,7 +1494,7 @@ function Invoke-NinjaOneTenantSync {
14941494
$Null = Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/related-items/entity/DOCUMENT/$($($MatchedLicDoc.documentId))/relations" -Method POST -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json' -Body ($Relations | ConvertTo-Json -Depth 100 -AsArray) -EA Stop
14951495
Write-Information 'Completed Update'
14961496
}
1497-
} Catch {
1497+
} catch {
14981498
Write-Information "Creating Relations Failed: $_"
14991499
}
15001500

@@ -2126,7 +2126,7 @@ function Invoke-NinjaOneTenantSync {
21262126
$CurrentItem | Add-Member -NotePropertyName lastStatus -NotePropertyValue 'Completed' -Force
21272127
Add-CIPPAzDataTableEntity @MappingTable -Entity $CurrentItem -Force
21282128

2129-
Write-LogMessage -API 'NinjaOneSync' -Headers'NinjaOneSync' -message "Completed NinjaOne Sync for $($Customer.displayName). Queued for $((New-TimeSpan -Start $StartQueueTime -End $StartTime).TotalSeconds) seconds. Data fetched in $((New-TimeSpan -Start $StartTime -End $FetchEnd).TotalSeconds) seconds. Total processing time $((New-TimeSpan -Start $StartTime -End (Get-Date)).TotalSeconds) seconds" -Sev 'info'
2129+
Write-LogMessage -tenant $Customer.defaultDomainName -API 'NinjaOneSync' -message "Completed NinjaOne Sync for $($Customer.displayName). Queued for $((New-TimeSpan -Start $StartQueueTime -End $StartTime).TotalSeconds) seconds. Data fetched in $((New-TimeSpan -Start $StartTime -End $FetchEnd).TotalSeconds) seconds. Total processing time $((New-TimeSpan -Start $StartTime -End (Get-Date)).TotalSeconds) seconds" -Sev 'info'
21302130

21312131
} catch {
21322132
$Message = if ($_.ErrorDetails.Message) {
@@ -2136,7 +2136,7 @@ function Invoke-NinjaOneTenantSync {
21362136
$_.Exception.message
21372137
}
21382138
Write-Error "Failed NinjaOne Processing for $($Customer.displayName) Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $Message"
2139-
Write-LogMessage -API 'NinjaOneSync' -Headers'NinjaOneSync' -message "Failed NinjaOne Processing for $($Customer.displayName) Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $Message" -Sev 'Error'
2139+
Write-LogMessage -tenant $Customer.defaultDomainName -API 'NinjaOneSync' -message "Failed NinjaOne Processing for $($Customer.displayName) Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $Message" -Sev 'Error'
21402140
$CurrentItem | Add-Member -NotePropertyName lastEndTime -NotePropertyValue ([string]$((Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ'))) -Force
21412141
$CurrentItem | Add-Member -NotePropertyName lastStatus -NotePropertyValue 'Failed' -Force
21422142
Add-CIPPAzDataTableEntity @MappingTable -Entity $CurrentItem -Force

0 commit comments

Comments
 (0)