Skip to content

Commit

Permalink
Merge pull request #21 from KelvinTegelaar/master
Browse files Browse the repository at this point in the history
[pull] master from KelvinTegelaar:master
  • Loading branch information
pull[bot] authored Jan 24, 2024
2 parents 784d7ac + 6d3e6ec commit 0379dd4
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 156 deletions.
31 changes: 30 additions & 1 deletion Modules/CIPPCore/Public/GraphHelper/Get-NormalizedError.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,42 @@ function Get-NormalizedError {
param (
[string]$message
)

#Check if the message is valid JSON.
try {
$JSONMsg = $message | ConvertFrom-Json
} catch {
}
#if the message is valid JSON, there can be multiple fields in which the error resides. These are:
# $message.error.Innererror.Message
# $message.error.Message
# $message.error.details.message
# $message.error.innererror.internalException.message

#We need to check if the message is in one of these fields, and if so, return it.
if ($JSONMsg.error.innererror.message) {
Write-Host 'innererror.message found'
$message = $JSONMsg.error.innererror.message
} elseif ($JSONMsg.error.message) {
Write-Host 'error.message found'
$message = $JSONMsg.error.message
} elseif ($JSONMsg.error.details.message) {
Write-Host 'error.details.message found'
$message = $JSONMsg.error.details.message
} elseif ($JSONMsg.error.innererror.internalException.message) {
Write-Host 'error.innererror.internalException.message found'
$message = $JSONMsg.error.innererror.internalException.message
}


#finally, put the message through the translator. If it's not in the list, just return the original message
switch -Wildcard ($message) {
'Request not applicable to target tenant.' { 'Required license not available for this tenant' }
"Neither tenant is B2C or tenant doesn't have premium license" { 'This feature requires a P1 license or higher' }
'Response status code does not indicate success: 400 (Bad Request).' { 'Error 400 occured. There is an issue with the token configuration for this tenant. Please perform an access check' }
'*Microsoft.Skype.Sync.Pstn.Tnm.Common.Http.HttpResponseException*' { 'Could not connect to Teams Admin center - Tenant might be missing a Teams license' }
'*Provide valid credential.*' { 'Error 400: There is an issue with your Exchange Token configuration. Please perform an access check for this tenant' }
'*This indicate that a subscription within the tenant has lapsed*' { 'There is no exchange subscription available, or it has lapsed. Check licensing information.' }
'*This indicate that a subscription within the tenant has lapsed*' { 'There is subscription for this service available, Check licensing information.' }
'*User was not found.*' { 'The relationship between this tenant and the partner has been dissolved from the tenant side.' }
'*The user or administrator has not consented to use the application*' { 'CIPP cannot access this tenant. Perform a CPV Refresh and Access Check via the settings menu' }
'*AADSTS50020*' { 'AADSTS50020: The user you have used for your Secure Application Model is a guest in this tenant, or your are using GDAP and have not added the user to the correct group. Please delete the guest user to gain access to this tenant' }
Expand Down
13 changes: 4 additions & 9 deletions Modules/CIPPCore/Public/GraphHelper/New-GraphPOSTRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ function New-GraphPOSTRequest ($uri, $tenantid, $body, $type, $scope, $AsApp, $N
try {
$ReturnedData = (Invoke-RestMethod -Uri $($uri) -Method $TYPE -Body $body -Headers $headers -ContentType $contentType)
} catch {
$Message = ($_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue).error
if ($Message.innerError) { $Message = $Message.Innererror.Message } else { $Message = $Message.Message.Error }
if ($Message -eq $null) {
try {
$Message = ($_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue).message
} catch {
$Message = $($_.Exception.Message)
}
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}
throw $Message
}
return $ReturnedData
} else {
Write-Error 'Not allowed. You cannot manage your own tenant or tenants not under your scope'

}
}
10 changes: 8 additions & 2 deletions Modules/CippExtensions/NinjaOne/Get-NinjaOneOrgMapping.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ function Get-NinjaOneOrgMapping {
$After = 0
$PageSize = 1000
$NinjaOrgs = do {
$Result = (Invoke-WebRequest -uri "https://$($Configuration.Instance)/api/v2/organizations?pageSize=$PageSize&after=$After" -Method GET -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json').content | ConvertFrom-Json -depth 100
$Result = (Invoke-WebRequest -Uri "https://$($Configuration.Instance)/api/v2/organizations?pageSize=$PageSize&after=$After" -Method GET -Headers @{Authorization = "Bearer $($token.access_token)" } -ContentType 'application/json').content | ConvertFrom-Json -Depth 100
$Result | Select-Object name, @{n = 'value'; e = { $_.id } }
$ResultCount = ($Result.id | Measure-Object -Maximum)
$After = $ResultCount.maximum

} while ($ResultCount.count -eq $PageSize)

} catch {
$NinjaOrgs = @()
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}

$NinjaOrgs = @(@{ name = $Message })
}

$MappingObj = [PSCustomObject]@{
Expand Down
14 changes: 12 additions & 2 deletions Modules/CippExtensions/NinjaOne/Get-NinjaOneToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Get-NinjaOneToken {

if (!$ENV:NinjaClientSecret) {
$null = Connect-AzAccount -Identity
$ClientSecret = (Get-AzKeyVaultSecret -VaultName $ENV:WEBSITE_DEPLOYMENT_ID -Name "NinjaOne" -AsPlainText)
$ClientSecret = (Get-AzKeyVaultSecret -VaultName $ENV:WEBSITE_DEPLOYMENT_ID -Name 'NinjaOne' -AsPlainText)
} else {
$ClientSecret = $ENV:NinjaClientSecret
}
Expand All @@ -20,7 +20,17 @@ function Get-NinjaOneToken {
scope = 'monitoring management'
}

$token = Invoke-RestMethod -Uri "https://$($Configuration.Instance -replace '/ws','')/ws/oauth/token" -Method Post -Body $body -ContentType 'application/x-www-form-urlencoded'
try {

$token = Invoke-RestMethod -Uri "https://$($Configuration.Instance -replace '/ws','')/ws/oauth/token" -Method Post -Body $body -ContentType 'application/x-www-form-urlencoded'
} catch {
$Message = if ($_.ErrorDetails.Message) {
Get-NormalizedError -Message $_.ErrorDetails.Message
} else {
$_.Exception.message
}
Write-LogMessage -Message $Message -sev error -API 'NinjaOne'
}
return $token

}
15 changes: 10 additions & 5 deletions Modules/CippExtensions/NinjaOne/Invoke-NinjaOneDeviceWebhook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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 'NinjaFieldMapping'"
Expand Down Expand Up @@ -36,9 +36,9 @@ function Invoke-NinjaOneDeviceWebhook {
"$($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'
$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"
Write-Host 'Updated NinjaOne Device Compliance'


} else {
Expand All @@ -48,8 +48,13 @@ function Invoke-NinjaOneDeviceWebhook {
}

} catch {
Write-Error "Failed NinjaOne Device Webhook for: $($Data | ConvertTo-Json -depth 100) Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $($_.Exception.message)"
Write-LogMessage -API 'NinjaOneSync' -user 'CIPP' -message "Failed NinjaOne Device Webhook Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $($_.Exception.message)" -Sev 'Error'
$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'
}


Expand Down
Loading

0 comments on commit 0379dd4

Please sign in to comment.