none
C# - How to execute multiple Powershell commands one after the other RRS feed

  • Question

  • Hi,

    I'm trying to write a C# application to remotely connect into Exchange 2010 and run some Powershell commands (via a remote Runspace). What I need to do is:

    1. Run the enable-mailbox command
    2. Let this command finish and check for errors
    3. Run the set-mailbox command
    4. Let this command finish and check for errors

    Could someone let me know what is the best way to code this up to allow the commands to run one after each other? What I currently have is below, but just unsure how to run multiple commands.

     

    Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
    
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    
    command.AddCommand("Enable-Mailbox");
    command.AddParameter("Identity", "testaccount");
    
    ArrayList emailAddresses = new ArrayList();
    emailAddresses.Add("SMTP:" + "testaccount@test.com.au");
    emailAddresses.Add("smtp:" + "testaccount@test.com");
       
    command.AddCommand("Set-Mailbox");
    command.AddParameter("Identity", "testaccount");
    command.AddParameter("EmailAddressPolicyEnabled", false);
    command.AddParameter("EmailAddresses", emailAddresses);
        
    powershell.Commands = command;
    
    try
    {
     runspace.Open();
     powershell.Runspace = runspace;
     powershell.Invoke();
    }
    

    Thanks.

    Tuesday, August 2, 2011 6:24 AM

All replies

  • Hi,

    You can add the script instead of adding a command.

    Please refer the following code :

     

      private void button1_Click(object sender, EventArgs e)  //Perform the scrip after click the button I added.
            {

                textBox1.Text = ""; //This textbox is used to add the script and show the output of the script.

                Collection<PSObject> results;
                PSSnapInException snapEx = null;

                       Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();

                pipeline.Commands.AddScript("di");
                pipeline.Commands.Add("out-string");

      try
                {

                    results = pipeline.Invoke();
                    StringBuilder sb = new StringBuilder();
                    foreach (PSObject obj in results)
                    {

                        outputBox1.AppendText(obj.ToString());

     

                    }

                 
                    outputBox1.ForeColor = Color.Black;

                               }
                catch
                {
                 
                         outputBox1.ForeColor = Color.Red;

                       
                        outputBox1.Text=pipeline.PipelineStateInfo.Reason.ToString();
                     
                 
                   
                }
                runspace.Close();

    }

     

     



    Tuesday, August 2, 2011 8:59 AM
    Moderator
  • Hi,

     

    Unfortunately I would prefer to run each command in the code (as first stated) than running a script as you suggested.

    Tuesday, August 2, 2011 9:19 AM
  • how can i use pipe line in above code ?

    for example get-mailbox | disable-mailbox

     I assume it's something like below but uses for each loop for commandresults

    ---------------------
    psCommand = "Enable-Mailbox"
        Dim myCommand As New Command(psCommand)
        myCommand.Parameters.Add("Identity", "domain\username")
        myCommand.Parameters.Add("Alias", "username")
        myCommand.Parameters.Add("Database", "server\site\mailbox")
        myCommand.Parameters.Add("DomainController", "local-dc")
        ' Add PS command to the pipeline
        pipeLine.Commands.Add(myCommand)

        Dim commandResults As ICollection(Of PSObject) = Nothing
        ' Run the pipeline command
        commandResults = pipeLine.Invoke
    .....


    Open source IT community: http://www.infotechguyz.com
    Saturday, August 6, 2011 6:59 AM
  • Hi, For running the powershell commands one after another you can use the Thread.Sleep(); where you can execute and pause for some time and execute another command. Hope this helps. Thanks, Dharma
    Dotnet
    Tuesday, January 3, 2012 12:53 PM