简体   繁体   English

将引号传递给 c# 代码中的 powershell 脚本不起作用

[英]Passing quotes to powershell script in c# code doesn't work

I have written c# code which executes powershell scripts with arguments.我已经编写了 c# 代码,它使用 arguments 执行 powershell 脚本。 It works great typically.它通常工作得很好。 But, when I try to pass the method an argument that has quotes, such as for a full name, it doesn't seem to recognize the quotes, only the first token.但是,当我尝试向该方法传递带有引号的参数时,例如全名,它似乎无法识别引号,只能识别第一个标记。

Here's how I'm creating the script arguments.下面是我如何创建脚本 arguments。 You can see that the fullName variable should be surrounded by quotes in the resulting scriptArguments....您可以看到 fullName 变量应在生成的 scriptArguments 中用引号引起来。...

  string scriptArguments = $@"-Phone {phoneNumber} -Name ""{fullName}""";
  var result = ExecutePowerShellScript(null, "MyScript.ps1", scriptArguments);

When I look at scriptArguments variable in debug it does appears correctly.当我在调试中查看 scriptArguments 变量时,它确实显示正确。

  -Phone 321-555-1111 -Name "John Smith"

I can even manually run the shell script and copy/paste the result of scriptArguments in the command line and the ps1 runs perfectly.我什至可以手动运行 shell 脚本并在命令行中复制/粘贴 scriptArguments 的结果,ps1 运行完美。 Such as....如....

  .\MyScript.ps1 -Phone 321-555-1111 -Name "John Smith"

But, when I allow the ExecutePowerShellScript method to run the script it only recognizes the first token of fullName, John.但是,当我允许 ExecutePowerShellScript 方法运行脚本时,它只识别全名 John 的第一个标记。 Here's the ExecutePowerShelScript method, in part...这是 ExecutePowerShelScript 方法,部分...

public PowerShellExecutionResult ExecuteScript(string scriptToExecute, string scriptArgument)
    {
        scriptToExecute = ConfigurationManager.AppSettings[“myscriptpath"] + scriptToExecute;

        try
        {
            using (var process = new Process
                {
                    StartInfo =
                    {
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        FileName = ConfigurationManager.AppSettings["powershell-exe-“path],
                        Arguments = $"{scriptToExecute} {scriptArgument}"
                    }
                })
            {
                process.Start();

                logCapture.Add(process.StandardOutput.ReadToEnd());

                process.WaitForExit();
            }

Basically the shell script tells me that "John" already exists in the service database.基本上 shell 脚本告诉我“John”已经存在于服务数据库中。 If anything, it should say "John Smith", not just the first token of the string.如果有的话,它应该说“John Smith”,而不仅仅是字符串的第一个标记。 Again, if I run this manually it works like a charm.同样,如果我手动运行它,它就像一个魅力。 So that would indicate that the problem is not in the shell script itself.所以这表明问题不在于 shell 脚本本身。

Any idea what is going on?知道发生了什么吗? I believe I'm doing everything correctly.我相信我做的一切都是正确的。

Try to escape the fullName with triple quotes:尝试用三引号转义 fullName:

string scriptArguments = $"-Phone {phoneNumber} -Name \"\"\"{fullName}\"\"\"";

Triple-escaped quotation marks are necessary to include the character in the final argument.三重转义引号对于在最后一个参数中包含字符是必需的。 See here .这里

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

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