Skip to content

Commit

Permalink
Merge pull request #1142 from JohnDuprey/dev
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
JohnDuprey authored Oct 10, 2024
2 parents 4720827 + 67c266a commit 441a910
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 32 deletions.
7 changes: 7 additions & 0 deletions CIPPTimers.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
"PreferredProcessor": "auditlog",
"IsSystem": true
},
{
"Command": "Start-ApplicationOrchestrator",
"Description": "Orchestrator to process application uploads",
"Cron": "0 0 */12 * * *",
"Priority": 2,
"RunOnProcessor": true
},
{
"Command": "Start-WebhookOrchestrator",
"Description": "Orchestrator to process webhooks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Function Push-ExecOnboardTenantQueue {
$Logs.Add([PSCustomObject]@{ Date = Get-Date -UFormat $DateFormat; Log = 'Clearing tenant cache' })
$y = 0
do {
$Tenant = Get-Tenants -TriggerRefresh -IncludeAll | Where-Object { $_.customerId -eq $Relationship.customer.tenantId } | Select-Object -First 1
$Tenant = Get-Tenants -TriggerRefresh -TenantFilter $Relationship.customer.tenantId | Select-Object -First 1
$y++
Start-Sleep -Seconds 20
} while (!$Tenant -and $y -le 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function Invoke-ExecAppUpload {
}
}

$Results = [pscustomobject]@{'Results' = 'Started application queue' }
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $Results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ function Invoke-ListGraphRequest {
$GraphRequestParams.AsApp = $true
}

Write-Host ($GraphRequestParams | ConvertTo-Json)

$Metadata = $GraphRequestParams

try {
$Results = Get-GraphRequestList @GraphRequestParams

if ($Results.nextLink -and $Request.Query.NoPagination) {
$Metadata['nextLink'] = $Results.nextLink | Select-Object -Last 1
#Results is an array of objects, so we need to remove the last object before returning
$Results = $Results | Select-Object -First ($Results.Count - 1)
}
if ($Request.Query.ListProperties) {
$Columns = ($Results | Select-Object -First 1).PSObject.Properties.Name
$Results = $Columns | Where-Object { @('Tenant', 'CippStatus') -notcontains $_ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Start-ApplicationOrchestrator {
Param()

Write-LogMessage -API 'IntuneApps' -message 'Started uploading applications to tenants' -sev Info

Write-Information 'Started uploading applications to tenants'
$InputObject = [PSCustomObject]@{
OrchestratorName = 'ApplicationOrchestrator'
SkipLog = $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ function Start-CIPPProcessorQueue {

foreach ($QueueItem in $QueueItems) {
if ($PSCmdlet.ShouldProcess("Processing function $($QueueItem.ProcessorFunction)")) {
Remove-AzDataTableEntity @QueueTable -Entity $QueueItem
$Parameters = $QueueItem.Parameters | ConvertFrom-Json -AsHashtable
if (Get-Command -Name $QueueItem.FunctionName -Module CIPPCore -ErrorAction SilentlyContinue) {
& $QueueItem.FunctionName @Parameters
Write-Information "Running queued function $($QueueItem.ProcessorFunction)"
if ($QueueItem.Parameters) {
try {
$Parameters = $QueueItem.Parameters | ConvertFrom-Json -AsHashtable
} catch {
$Parameters = @{}
}
} else {
$Parameters = @{}
}
if (Get-Command -Name $QueueItem.ProcessorFunction -Module CIPPCore -ErrorAction SilentlyContinue) {
try {
Invoke-Command -ScriptBlock { & $QueueItem.ProcessorFunction @Parameters }
} catch {
Write-Warning "Failed to run function $($QueueItem.ProcessorFunction). Error: $($_.Exception.Message)"
}
} else {
Write-Warning "Function $($QueueItem.FunctionName) not found"
Write-Warning "Function $($QueueItem.ProcessorFunction) not found"
}
Remove-AzDataTableEntity @QueueTable -Entity $QueueItem
}
}
}
5 changes: 3 additions & 2 deletions Modules/CIPPCore/Public/GraphHelper/Write-LogMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ function Write-LogMessage {
'Username' = [string]$username
'Severity' = [string]$sev
'SentAsAlert' = $false
'PartitionKey' = $PartitionKey
'RowKey' = ([guid]::NewGuid()).ToString()
'PartitionKey' = [string]$PartitionKey
'RowKey' = [string]([guid]::NewGuid()).ToString()
'FunctionNode' = [string]$env:WEBSITE_SITE_NAME
'LogData' = [string]$LogData
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,6 @@ function Get-GraphRequestList {
if ($nextLink) { $GraphRequest.uri = $nextLink }

$GraphRequestResults = New-GraphGetRequest @GraphRequest -Caller 'Get-GraphRequestList' -ErrorAction Stop
if ($GraphRequestResults.nextLink) {
#$Metadata['nextLink'] = $GraphRequestResults.nextLink | Select-Object -Last 1
#GraphRequestResults is an array of objects, so we need to remove the last object before returning
$GraphRequestResults = $GraphRequestResults | Select-Object -First ($GraphRequestResults.Count - 1)
}
$GraphRequestResults = $GraphRequestResults | Select-Object *, @{n = 'Tenant'; e = { $TenantFilter } }, @{n = 'CippStatus'; e = { 'Good' } }

if ($ReverseTenantLookup -and $GraphRequestResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ function Invoke-NinjaOneDeviceWebhook {
$Configuration
)
try {
Write-LogMessage -user $ExecutingUser -API $APIName -message "Webhook Recieved - Updating NinjaOne Device compliance for $($Data.resourceData.id) in $($Data.tenantId)" -Sev 'Info' -tenant $TenantFilter
$MappedFields = [pscustomobject]@{}
$CIPPMapping = Get-CIPPTable -TableName CippMapping
$Filter = "PartitionKey eq 'NinjaOneFieldMapping'"
Expand All @@ -14,6 +13,7 @@ function Invoke-NinjaOneDeviceWebhook {
}

if ($MappedFields.DeviceCompliance) {
Write-LogMessage -user $ExecutingUser -API $APIName -message "Webhook Recieved - Updating NinjaOne Device compliance for $($Data.resourceData.id) in $($Data.tenantId)" -Sev 'Info' -tenant $TenantFilter
$tenantfilter = $Data.tenantId
$M365DeviceID = $Data.resourceData.id

Expand All @@ -24,23 +24,36 @@ function Invoke-NinjaOneDeviceWebhook {
$Device = Get-CIPPAzDataTableEntity @DeviceMapTable -Filter $DeviceFilter

if (($Device | Measure-Object).count -eq 1) {
$Token = Get-NinjaOneToken -configuration $Configuration
try {
$Token = Get-NinjaOneToken -configuration $Configuration

if ($DeviceM365.isCompliant -eq $True) {
$Compliant = 'Compliant'
} else {
$Compliant = 'Non-Compliant'
}

$ComplianceBody = @{
"$($MappedFields.DeviceCompliance)" = $Compliant
} | ConvertTo-Json
if (!$Token.access_token) {
Write-LogMessage -API 'NinjaOneSync' -tenant $tenantfilter -user 'CIPP' -message 'Failed to get NinjaOne Token for Device Compliance Update' -Sev 'Error'
return
}

$Null = Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/device/$($Device.NinjaOneID)/custom-fields" -Method PATCH -Body $ComplianceBody -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json'
if ($DeviceM365.isCompliant -eq $True) {
$Compliant = 'Compliant'
} else {
$Compliant = 'Non-Compliant'
}

Write-Host 'Updated NinjaOne Device Compliance'
$ComplianceBody = @{
"$($MappedFields.DeviceCompliance)" = $Compliant
} | ConvertTo-Json

$Null = Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/device/$($Device.NinjaOneID)/custom-fields" -Method PATCH -Body $ComplianceBody -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json'

Write-Host 'Updated NinjaOne Device Compliance'
} catch {
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}
Write-Error "Failed NinjaOne Device Webhook for: $($Data | ConvertTo-Json -Depth 100) Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $Message"
Write-LogMessage -API 'NinjaOneSync' -user 'CIPP' -message "Failed NinjaOne Device Webhook Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $Message" -Sev 'Error'
}
} else {
Write-LogMessage -API 'NinjaOneSync' -user 'CIPP' -message "$($DeviceM365.displayName) ($($M365DeviceID)) was not matched in Ninja for $($tenantfilter)" -Sev 'Info'
}
Expand All @@ -59,4 +72,4 @@ function Invoke-NinjaOneDeviceWebhook {



}
}

0 comments on commit 441a910

Please sign in to comment.