0

I can't seem to run a command like this one...

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

...without getting an error saying that it cannot find the module. It's expecting me to use something like

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

How can I use that format with a command like this?

1 Answer 1

3

Try AddScript instead.

      PowerShell shell = PowerShell.Create().AddScript(String.Format("(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(\"{0}\")",strShare ));
Sign up to request clarification or add additional context in comments.

4 Comments

Tried that, just tried it again. It gives me System.Management.Automation.ParseException with a bunch of comments about "unexpected token". I know the command works at the powershell command line.
fixed quotes. sorry about that
My bad, another set of escaped quotes was needed.
Here's what I did instead, not sure if this could be written better: PowerShell pshell = PowerShell.Create().AddScript(String.Format("(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection(\"{0}\")", strShare));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.