M365 CLI commands with ConvertFrom-Json – Invalid JSON Primitive: -.
This is going to be a quick troubleshooting blog(note to self), Recently we were trying to create an automation script to export the chat conversation as an HTML file and were keen to use M365 CLI.
I had already submitted a script to export Teams’s channel conversation to html file which can be found here.
As we will try this, in one of the machines, we were getting errors while using the ConvertFrom-Json command, Below are commands which we were trying
m365 teams chat get --participants "bruce@waynecorporation.com" -o json | ConvertFrom-Json
$messages = m365 teams chat message list --chatId 19:4e8be37c-0b8d-4959-9288-1723738934_cd15fc37-4694-4f34-811f-965ca5cc586a@unq.gbl.spaces -o json | ConvertFrom-Json -AsHashtable
Below are errors which we were getting.
Error 1 “ConvertFrom-Json: Invalid JSON primitive: – ”
Error 2 on another command – “ConvertFrom-Json: Conversion from JSON failed with error: Input String ”-” is not a valid number. Path ”, line1 , position 1
Error 1 Reference screenshot
Error 2 Reference screenshot
Strange this is that this command was working fine in another machine, which got curios thinking what is issue with this machine. Initially our thoughts was going that something is wrong with ConvertFrom-Json command on that machine and so we tried different ways to check how it works and behaves if giving normal json object reading from text etc and it worked fine,
After 30 mins of scratching our heads, one thing we noticed on that machine is that whenever we were running the M365 CLIi command there was this spinner message coming in a shell window
“- Running command …”
Now we knew what was happening, so we tried to run the CLI command and output it to an txt file and found that this text “- Running command …” is the first line coming and then the actual response JSON object.
Now we knew what is our culprit and wanted to find a way to disable this, that is where we found an issue logged in CLI repo. and thanks for our awesome contributor @Martin Lingstuyl who has also faced a similar issue with Azure functions and applied a fix in CLI to enable/disable this spinner.
So below is what you have to do to disable the spinner
m365 cli config set --key showSpinner --value false 2>$null
And Wola, that’s it after running this command, we were able to run the original commands.
Hope this helps, happy coding..!!!1