0

I'm working in some Windows Form C# project, where I need to launch the next command (which works perfectly in the PoweShell):

Invoke-RestMethod -Method Post -Uri http://<my_domain>/api/v4/groups/ - 
Headers @{"PRIVATE-TOKEN"="<my_token>"} -ContentType 
"application/json" -Body '{"path": "<my_group_name>", "name": " 
<my_group_name>", "parent_id": 5 }'

The matter is that I need to launch that command from Visual Studio using:

using (PowerShell powershell= PowerShell.Create())
{  
   powershell.AddScript();
}

My code #1:

using (powershell = PowerShell.Create())
{
   var res = powershell.AddScript(@"Invoke-RestMethod -Method Post -Uri 
   http://<my_domain>/api/v4/groups/ -Headers @{" + "\"PRIVATE- 
   TOKEN\"=\"<my_token>\"} -ContentType \"application/json\" -Body 
   '{\"path\": \"<my_group_name>\", \"name\": \"<my_group_name>\", 
   \"parent_id\": 5 }'");
}

Result #1 (doesn't work, but the variable 'res' seems to be OK):

res = Invoke-RestMethod -Method Post -Uri http://my_domain/api/v4/groups/ 
-Headers @{"PRIVATE-TOKEN"="my_token"} -ContentType 
"application/json" -Body '{"path": "my_group_name", "name": "my_group_name", 
"parent_id": 5 }'

My code #2 (Multiline string literal with doble quotes):

using (powershell = PowerShell.Create())
{
    string msk = @"
    $header = '""PRIVATE-TOKEN""=""<my_token>""'
    $content = '""application/json""'
    $body = '{""path"": ""<my_group_name>"", ""name"": ""<my_group_name>"", 
    ""parent_id"": 5 }'
    Invoke -RestMethod -Method Post -Uri http://<my_domain>/api/v4/groups/ - 
    Headers @{$header} -ContentType $content -Body $body";

    powershell.AddScript(msk);
}

Result #2:

"$header = '\"PRIVATE-TOKEN\"=\"iT13vYq4jwg17Agyxsdz\"
 $content = '\"application/json\"'
 $body = '{\"path\": \"sss\", \"name\": \"sss\", \"parent_id\": 5 }'                  
 Invoke -RestMethod -Method Post -Uri http://172.24.3.253/api/v4/groups/     
 -Headers @{$header} -ContentType $content -Body $body"

I've seen this same problem in here, but can't reach my goal.

Using double quotes in PowerShell script executed from C# application

Besides, I've been looking at similar problems, without getting anything.

I must be doing some mistake that I don't notice. Could anyone help me resolving this issue?

Appreciate your help.

kader
  • 15
  • 1
  • 5

1 Answers1

0

Try this,

using (PowerShell shell = PowerShell.Create())
{
    IDictionary dict = new Dictionary<string, string>(); // -Headers parameter needs Dictionary values, string @{} won't work.
    dict.Add("PRIVATE_TOKEN", "something");

    shell.AddCommand("Invoke-RestMethod").AddParameter("Method", "Post")
          .AddParameter("Uri", "http://test.com/api/v4/groups/")
           .AddParameter("Headers", dict).AddParameter("ContentType", "application/json")
            .AddParameter("Body", "'{'path': '<my_group_name>', 'name': '<my_group_name>','parent_id': 5 }'");
             shell.Invoke();
}
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
  • I've obtained 'System.Management.Automation.CmdletInvocationException' @Berkay. Error Http 400, Bad Request. I'm searching for information to resolve it. – kader Mar 01 '21 at 08:37
  • Checj json params and property types. Something might not match @kader – Berkay Yaylacı Mar 01 '21 at 08:48