2013-12-16 30 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