1

I would like to create a console program like

Invoke-RestMethod -Body $body

to pass a json content using powershell on windows.

[string] $body = '
                  {
                    "a": {
                          "b" : "foo bar",
                          "c" : true
                          "d" :{
                                 "p": "refs/heads/master"
                               }
                         }
                  }
                  '
.\ConsoleApp.exe --Body $body

The C# console program to display command line arguments :

static void Main(string[] args)
{
    Console.WriteLine("----------------");
    Console.WriteLine("COUNT = " + args.Length);
    Console.WriteLine("----------------");
    foreach (var item in args)
    {
        Console.WriteLine(item);
        Console.WriteLine("----------------");
    }
}

Here the result : ![enter image description here

As you can see, the json content is split in 2 arguments So my question is :

Using Powershell, how can I pass a json string with spaces, double quotes and line breaks as a single argument value to my program ?

Thanks for advance

Sybaris
  • 41
  • 5
  • The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3,0 with selective exceptions on Windows, for now _by default_ - though a future version may revert to the old, broken behavior, requiring an _opt-in_ for the fix. See the linked duplicate for details. – mklement0 Feb 28 '23 at 23:10

0 Answers0