2014-03-12 20 views
0

我似乎無法對這樣一個運行一個命令如何更復雜的PowerShell命令添加到管道

pshell.AddCommand("(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(" + strShare + ")"); 

...沒有得到一個錯誤,指出它無法找到該模塊。它期待我使用類似於

pshell.AddCommand("New-Object"); 
pshell.AddArgument("-ComObject"); 
pshell.Invoke(); 

我該如何使用這種格式的命令?

回答

3

嘗試AddScript代替。

 PowerShell shell = PowerShell.Create().AddScript(String.Format("(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(\"{0}\")",strShare)); 
+0

試過了,剛試了一遍。它給我System.Management.Automation.ParseException與一堆關於「意外的令牌」的評論。我知道這個命令在powershell命令行中有效。 –

+0

固定報價。對不起, – DeveloperGuo

+0

仍似乎得到相同的錯誤: –