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.