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!