【问题标题】:Passing powershell multiple arguments in C# using Process.Start使用 Process.Start 在 C# 中传递 powershell 多个参数
【发布时间】:2020-07-02 03:12:33
【问题描述】:

我正在从控制台应用程序调用 PowerShell 脚本。我需要在网络共享中调用一个脚本并传递几个参数。我需要帮助构建论点。我尝试使用 @ 符号来精确化参数。这是代码的截图:

 Process process = new Process();
        // Configure the process using the StartInfo properties.
        process.StartInfo.FileName = "powershell.exe";
        process.StartInfo.Arguments = "powershell.exe -ExecutionPolicy Bypass -Noninteractive -File "\\Share\ConfigScript.ps1" -Config "\\Share\Config.xml" -Webservice "https://companysite.net/ConfigSite"";
        process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        process.Start();
        process.WaitForExit();// Waits here for the process to exit. 

如果我按照它们在 PowerShell 中的工作方式复制/粘贴参数,我会收到多个 CS1002 和 CS1056 错误。

编辑:注意:参数中的 powershell.exe 用于绕过执行策略

【问题讨论】:

标签: c# powershell console-application process.start


【解决方案1】:

你需要对字符串中的特殊字符进行转义。

\\会给你\

\" 会给你"

请注意,这些特殊字符使用 \ 字符进行转义。另外,请检查您是否有意在参数中包含 powershell.exe,因为它已被指定为文件名?

【讨论】:

  • 参数中的powershell.exe用于绕过执行策略
  • 好的。然后如前所述,继续根据 c# 规范正确转义您的字符串。
猜你喜欢
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多