Search code examples
c#powershellactivesync

C# and powershell multiple command


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?


Solution

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