I came across this powershell quirk today and thought I should share it with anyone else who might be writing powershell ansible modules.
It seems there is a bug, or at least an inconsistency in the behaviour of ConvertTo-Json
It appears that it fails to convert strings to json if they end with a trailing backslash, which is a hazard for windows folder paths which can be expressed with a trailing backslash, e.g c:\temp\
What is strange is that if you use the -Compress option with ConvertTo-Json the string seems to convert ok.
I wound up using
$message = $message.TrimEnd("") to clean up the affected string in my custom module, but I’m considering whether it would be wise to add -Compress to the two places it occurs in lib/ansible/module_utils/powershell.ps1. This would make ansible a bit more robust to modules without sanitized output - I suppose at the expense of the overhead of ‘compressing’ the json (which seems to just be quoting and removing whitespace).
The issue is described here:
All the best,
Jon