Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

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

I have written c# code which executes powershell scripts with arguments. 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. You can see that the fullName variable should be surrounded by quotes in the resulting 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.

  -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! 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. Here's the ExecutePowerShelScript method, in part...

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. If anything, it should say "John Smith", not just the first token of the string. 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.

Any idea what is going on? I believe I'm doing everything correctly.

question from:https://stackoverflow.com/questions/65850522/passing-quotes-to-powershell-script-in-c-sharp-code-doesnt-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to escape the fullName with triple quotes:

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

Triple-escaped quotation marks are necessary to include the character in the final argument. See here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...