0

I just want to make sure it's not some behavior of some powershell command that causes this issue.

I make an API call and for some reason when a JSON object is too long, it just gets cut off. This results in incomplete JSON, breaking the rest of the file.

(Microsoft.Exchange API)

(Search-UnifiedAuditLog -StartDate $startDateTime -EndDate $endDateTime -RecordType $recordType | Select-Object AuditData).AuditData | Out-File .\API_Dump_Broken.json

I tested it with as few code as possible, to make sure it's not an issue with my code.

(Fake) example of the JSON result:

{"CreationTime":"2019-02-16T12:50:37","Id":"068ca975-72a1-40c4-171d-08d6959fae97","Name":"Formaldehyde"}
{"CreationTime":"2019-02-17T12:50:37","Id":"068ca975-72a1-40c4-171d-08d6959fae97","Name":"OxyMethylene", "Language": "Powershell", "Message": "This is getting cut o
{"CreationTime":"2019-02-18T12:50:37","Id":"068ca975-72a1-40c4-171d-08d6959fae97","Name":"Methanal"}

The second record is getting cut off, while the others are technically fine. Ofcourse the 3rd record is now broken, because the second one got cut off.

Does Select-Object or Out-File limit the amount of characters returned in any way? Or is this really an issue of the API just cutting off the JSON objects and can I go complain to the devs :)

Thanks in advance!

Michael

Michael B.
  • 558
  • 3
  • 11
  • Have a look at https://stackoverflow.com/questions/16854057/convertfrom-json-max-length – Stuart Feb 18 '19 at 14:01
  • I know there is a limit on the ConvertFrom-Json, but I'm not even at the point of using that. My issue is that the API returns all the results in json objects as strings, but when these are too long he just cuts it off... resulting in invalid json. – Michael B. Feb 18 '19 at 14:08
  • Post the code that does your API call... – Stuart Feb 18 '19 at 14:32
  • Editted the post. I'm directly calling the Microsoft.Exchange API to read the audit log. – Michael B. Feb 18 '19 at 14:35
  • See [Out-File](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-6) and read what is written for the `Width` parameter. You may try with `Set-Content` – Theo Feb 18 '19 at 21:57
  • Thanks for the suggestion, but same result. – Michael B. Feb 19 '19 at 10:37
  • Try using `Out-File` with `-Width [int]::MaxValue` or uncompress the json using `(Search-UnifiedAuditLog -StartDate $startDateTime -EndDate $endDateTime -RecordType $recordType | Select-Object AuditData).AuditData | ConvertFrom-Json | ConvertTo-Json -Depth 100`. P.S. Please 'ping' me when addressing a comment (@Theo) – Theo Feb 19 '19 at 14:48

0 Answers0