简体   繁体   English

如何从 Visual Studio (C#) 启动 PowerShell 脚本

[英]How to launch PowerShell script from Visual Studio (C#)

I'm working in some Windows Form C# project, where I need to launch the next command (which works perfectly in the PoweShell):我在一些 Windows Form C# 项目中工作,我需要在其中启动下一个命令(在 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:问题是我需要使用以下命令从 Visual Studio 启动该命令:

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

My code #1:我的代码#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):结果#1(不起作用,但变量“res”似乎没问题):

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):我的代码#2(带双引号的多行字符串文字):

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:结果#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 在从 C# 应用程序执行的 PowerShell 脚本中使用双引号

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.感谢你的帮助。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM