[英]Pass PowerShell arguments to Process.Start
I am trying to convert some of my bat scripts over to C#. 我试图将我的一些bat脚本转换为C#。 I have the entire program ported except for the PowerShell execution line.
除了PowerShell执行行之外,我已移植整个程序。
To keep things simple, I was trying to just call the PowerShell command from cmd. 为了简单起见,我试图从cmd调用PowerShell命令。 I am able to do so but its not taking the complex arguments and parameters that go along with the PowerShell command.
我能够这样做,但它没有采用与PowerShell命令一起的复杂参数和参数。
Here is the command if I run directly from cmd or bat file. 如果我直接从cmd或bat文件运行,这是命令。
powershell -Command "& {asnp 'citrix*'; remove-BrokerTag -Name 'Somename' -Machine 'domain\server' -AdminAddress 'SomeServer';}"
I was using this code to invoke cmd and run my powershell command. 我正在使用此代码调用cmd并运行我的powershell命令。
string strCmdText;
strCmdText = "'/C powershell " + "-Command " + "&" + " { asnp 'citrix*'; add - BrokerTag - Name 'Somename' - Machine 'domain\server'" + (listboxvariable) + " - AdminAddress 'ServerXX'; }'" + " & pause";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
I get this error "Cannot process the command because of a missing parameter. A command must follow -Command." 我收到此错误“由于缺少参数,无法处理命令。命令必须遵循-Command。”
As you can see there are a lot of arguments in this PowerShell command. 正如您所看到的,这个PowerShell命令中有很多参数。
Is there an easier way to do this? 有更简单的方法吗? or I am just missing something simple?
或者我只是遗漏了一些简单的东西?
In your original PowerShell line, the Command
is surrounded by double quotes. 在原始的PowerShell行中,
Command
被双引号括起来。
Try this: 尝试这个:
strCmdText = "/C powershell " + "-Command " + "\"& " + "{ asnp 'citrix*'; add - BrokerTag - Name 'Somename' - Machine 'domain\server'" + (listboxvariable) + " - AdminAddress 'ServerXX'; }\"" + " & pause";
Notice the use of \\"
escape sequences to add the missing double quotes that are present in your original command line. 请注意使用
\\"
转义序列添加原始命令行中存在的缺失双引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.