views:

214

answers:

2

Hi,

I have a Windows Forms app that provisions user accounts in Exchange using Powershell and Exchange2007 cmdlets. There is only one form to this application that takes the information for the new user and then runs the Powershell commands. Like a good programmer, I just refactored the code and took all the Exchange and Active Directory calls out and put them in separate classes. In the Windows form, I call the following code in a button Click event:

ExchangeMailboxFunctions exFuncs = new ExchangeMailboxFunctions();

exFuncs.CreateNewMailbox(username, userPrincipalName, OU, txtDisplayName.Text, txtFirstName.Text, txtInitials.Text, txtLastName.Text,
   txtPassword1.Text, ckbUserChangePassword.Checked, mailboxLocation);

In the class itself, I have the following:

RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException warning;
Runspace thisRunspace;

public ExchangeMailboxFunctions()
    {
        InitializePowershell();
    }

    private void InitializePowershell()
    {
        try
        {
            thisRunspace = RunspaceFactory.CreateRunspace(config);
            config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
            thisRunspace.Open();
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("Could not initialize Powershell: {0}", ex.Message));
        }
    }

public void CreateNewMailbox(string username, string userPrincipalName, string OU, string DisplayName, string FirstName, string Initials,
        string LastName, string Password, bool ChangePasswordNextLogon, string MailboxLocation)
    {

        try
        {
            using (Pipeline thisPipeline = thisRunspace.CreatePipeline())

 [Set a bunch of pipLine parameters here]

 thisPipeline.Invoke();
    }
    catch (Exception ex)

The thisPipeline.Invoke causes the error and I have no idea what is Disposed. This code worked perfectly fine when it was in the codebehind of the form. I also have some Active Directory methods that I tore out into a separate class library and they seem to work fine.

What should I do to get this to stop happening? Thanks!

+2  A: 

Make sure your code actually looks like this...

using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
{
    [Set a bunch of pipLine parameters here]

    thisPipeline.Invoke();
}

The brackets being the key, and they're missing in your example.

Adam Robinson
Yup, this is what is going on. There also seems to be another missing "}" for the function.
JasonMArcher
A: 

Sorry guys, the brackets and such were actually ok - I must've left them out of the questions. I missed these lines of code in the create method:

 using (SecureString ss = new SecureString())
 {
    foreach (char c in Password)
      ss.AppendChar(c);
    thisPipeline.Commands[0].Parameters.Add("Password", ss);
 }

Since the Using disposed before the Invoke got called, there was no ss as it was already Disposed. Should've looked a little deeper before posting this :-(