3

I have two lines of power-shell command in the code below. Command 1 enables the mailbox, and command 2 disables the active sync.

PSCommand psCmd = new PSCommand();

command2 ="Enable-Mailbox -Identity \""+ ExchangeIdentity 
    +"\" -Database \""+ExchangeDBName+ "\" -DomainController \""
    +ExchangeDC+"\" -primarysmtpaddress \""+ExchangePrimarySMTPAddress +"\"";

command1 = "Set-CASMailbox -Identity \"" +ExchangePrimarySMTPAddress
    +"\" -ActiveSyncEnabled "+ActiveSync_Status;

allcommand = command2 + " |" + command1;

runspace.Open();

powershell.AddScript(allcommand);

powershell.Runspace = runspace;
ICollection<PSObject> iobj = powershell.Invoke();

When I try to run the command 1 alone in the add script, the mailbox is created successfully. When I combine it with the second, the mailbox alone is created but active sync is not disabled (if i pass the value as false).

How can I handle two commands in single line?

ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
GPR
  • 49
  • 1
  • 1
  • 5

4 Answers4

7

I would recommend not using AddScript if you are just invoking commands.

Assuming you want to invoke both commands but not pipe the results from the first to the second, and assuming you are using PowerShell V3, I would write the code like this:

powershell.AddCommand("Enable-Mailbox")
    .AddParameter("Identity", ExchangeIdentity)
    .AddParameter("Database", ExchangeDBName)
    .AddParameter("DomainController", ExchangeDC)
    .AddParameter("PrimarySmtpAddress ", ExchangePrimarySMTPAddress );
// AddStatement - like adding a ';' in a script - V3 only
powershell.AddStatement();
powershell.AddCommand("Set-CASMailbox")
    .AddParameter("Identity", ExchangePrimarySMTPAddress)
    .AddParameter("ActiveSyncEnabled", ActiveSync_Status);

Written this way (not using AddScript), you avoid one possible problem with script injection. If any of the arguments can come from a malicious user, it would be possible to construct an argument that closes one of the previous quotes, then adds malicious script and you'll run that script with admin privileges. Using AddCommand/AddParameter, you can avoid this situation.

Even if you control the arguments completely, you could still hit problems with some arguments if they contain characters like ';' and you weren't properly quoting the arguments.

Jason Shirk
  • 7,734
  • 2
  • 24
  • 29
  • +1 This should be the accepted answer. AddStatement() allows you to "batch up" multiple commands and call invoke only once. – Thiago Silva Jul 21 '16 at 16:21
1

Doesn't it work if you AddScript(command2) and AddScript(command1) to the pipeline and then Invoke them?

This should force powershell to run both commands in sequence after each other...

MichaC
  • 13,104
  • 2
  • 44
  • 56
  • sorry i tried with "| "and ";" and for the "|" the script was not executed and for ";" i got the mailbox created but active sync command didnt work. – GPR Sep 24 '13 at 12:52
  • 1
    I meant you should not combine both commands into one and then pass it to powershell, instead try to add BOTH scripts separately to powershell with AddScript and then Invoke them. Did you try that? – MichaC Sep 24 '13 at 12:54
1

For version 2.0

create c# code to invoke power shell and execute 1st command for mailbox creation then dispose and write another set of c# code to invoke and execute the Active sync command and dispose.

Note: the Domain controller should be the same for mailbox creation and Active sync.

GPR
  • 49
  • 1
  • 1
  • 5
0

According to the docs on the Enable-Mailbox command, it doesn't output anything. Forget the pipe. Change it to ';' to separate the two statements.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369