【发布时间】: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 用于绕过执行策略
【问题讨论】:
-
显然你有一个语法问题,最好你需要转义并使用逐字字符串
-
还有
process.StartInfo.Arguments应该只包含参数(删除powershell.exe) -
var escapedString = @"mystring";是你的朋友吗。
标签: c# powershell console-application process.start