Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

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.

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...

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top