2013-12-16 29 views
0

我有一个可以使用Microsoft.PowerShell.ConsoleShell.Start()启动PowerShell实例的工具。这很好,除了我不能通过将它作为-Command参数传递来运行脚本。带有命令参数的ConsoleShell

简化了代码看起来像这样

string[] psArgs = new[] {"Command", ".\\test.ps1"}; 

var config = RunspaceConfiguration.Create(); 
int i= ConsoleShell.Start(
    config, 
    "PSPowerShell", 
    "", 
    psArgs 
); 

当我用这个,PowerShell启动,我得到下面的输出

CommandType  Name  Definition 
-----------  ----  ---------- 
ExternalScript test.ps1 E:\testarea\test.ps1 

这感觉就像我失去了一些东西微不足道,但我看不到它...

我也试过使用&,但只给了我错误:

Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string. 

回答

0

我认为你的问题是你告诉它运行命令,但是然后你给它一个脚本文件。

试着改变你的第一行是:

string[] psArgs = new[] {"-File", ".\\test.ps1"}; 
+0

这个答案的工作,使我意识到,我已经错过了 - 。将第一行改为:'string [] psArgs = new [] {“-Command”,“。\\ test.ps1”};'它工作得很好。 – nimatt