5

I am trying to convert some of my bat scripts over to C#. I have the entire program ported except for the PowerShell execution line.

To keep things simple, I was trying to just call the PowerShell command from cmd. I am able to do so but its not taking the complex arguments and parameters that go along with the PowerShell command.

Here is the command if I run directly from cmd or bat file.

powershell -Command "& {asnp 'citrix*'; remove-BrokerTag -Name 'Somename' -Machine 'domain\server' -AdminAddress 'SomeServer';}"

I was using this code to invoke cmd and run my powershell command.

string strCmdText;
strCmdText = "'/C powershell " + "-Command " + "&" + " { asnp 'citrix*'; add - BrokerTag - Name 'Somename' - Machine 'domain\server'" + (listboxvariable) + " - AdminAddress 'ServerXX'; }'" + " & pause";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

I get this error "Cannot process the command because of a missing parameter. A command must follow -Command."

As you can see there are a lot of arguments in this PowerShell command.

Is there an easier way to do this? or I am just missing something simple?

1

1 Answer 1

4

In your original PowerShell line, the Command is surrounded by double quotes.

Try this:

strCmdText = "/C powershell " + "-Command " + "\"& " + "{ asnp 'citrix*'; add - BrokerTag - Name 'Somename' - Machine 'domain\server'" + (listboxvariable) + " - AdminAddress 'ServerXX'; }\"" + " & pause";

Notice the use of \" escape sequences to add the missing double quotes that are present in your original command line.

Sign up to request clarification or add additional context in comments.

6 Comments

i get the same error. --> Cannot process the command because of a missing parameter. A command must follow -Command. Followed by --> '"{ asnp 'citrix*'; add - BrokerTag - Name 'somename' - Machine 'domain'server - AdminAddress 'Server'; }"' is not recognized as an internal or external command,
@Robert Harvey♦ The original Command has a double quote before the & shouldn't the \" be inserted before the & instead of putting it after ?
@pnadalini: Fixed.
thanks!! .. the powershell command is actually running now.. but now a problem when trying to define the -Machine parameter. it seems to be ignoring the \ in domain\machine..
I guess you will have to double the backslash there too: domain\\server
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.