Skip to content

Commit 52b81aa

Browse files
committed
Improve error handling in New-GraphGetRequest
Enhanced the handling of JSON parsing in the raw response by wrapping Test-Json in a try/catch block to prevent errors from breaking execution. Also replaced Select-Object with a PSCustomObject for more explicit property assignment.
1 parent c10da45 commit 52b81aa

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Modules/CIPPCore/Public/GraphHelper/New-GraphGetRequest.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ function New-GraphGetRequest {
8787
$RequestSuccessful = $true
8888

8989
if ($ReturnRawResponse) {
90-
if (Test-Json -Json $Data.Content) {
91-
$Content = $Data.Content | ConvertFrom-Json
92-
} else {
90+
try {
91+
if ($Data.Content -and (Test-Json -Json $Data.Content -ErrorAction Stop)) {
92+
$Content = $Data.Content | ConvertFrom-Json
93+
} else {
94+
$Content = $Data.Content
95+
}
96+
} catch {
9397
$Content = $Data.Content
9498
}
9599

96-
$Data | Select-Object -Property StatusCode, StatusDescription, @{Name = 'Content'; Expression = { $Content } }
100+
[PSCustomObject]@{
101+
StatusCode = $Data.StatusCode
102+
StatusDescription = $Data.StatusDescription
103+
Content = $Content
104+
}
97105
$nextURL = $null
98106
} elseif ($CountOnly) {
99107
$Data.'@odata.count'

0 commit comments

Comments
 (0)