如何向管道添加更复杂的Powershell命令

我似乎无法运行像这样的命令……

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

…没有收到错误,说它无法找到模块。 它期待我使用类似的东西

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

如何在这样的命令中使用该格式?

请尝试使用AddScript 。

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