2009-04-23 11 views
1

PowershellおよびExchange2007コマンドレットを使用してExchangeでユーザーアカウントをプロビジョニングするWindowsフォームアプリケーションがあります。このアプリケーションには、新しいユーザーの情報を取得してPowershellコマンドを実行するフォームが1つしかありません。良いプログラマのように、私はコードをリファクタリングして、ExchangeとActive Directoryのすべての呼び出しを取り出して別々のクラスに入れました。 Windowsフォームでは、私はボタンのClickイベントに次のコードを呼び出す:クラス自体で外部クラスライブラリ呼び出しで破棄されたオブジェクトエラーにアクセスできません

ExchangeMailboxFunctions exFuncs = new ExchangeMailboxFunctions(); 

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

、私は次のようしている:

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) 

をthisPipeline.Invokeは、エラーが発生し、私が持っています何が処分されているのか分かりません。このコードは、フォームのコードビハインドに含まれていたときに問題なく動作しました。私は別のクラスライブラリに流出したいくつかのActive Directoryメソッドもあり、うまく動作しているようです。

これをやめるにはどうすればよいですか?ありがとう!

答えて

2

はブラケットがキーであること、そして、彼らはあなたの例では不足している...

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

    thisPipeline.Invoke(); 
} 

をあなたのコードは、実際にこのようになりますことを確認してください。

+0

うん、これは何が起こっているかです。 – JasonMArcher

0

申し訳ありませんが、大括弧などは実際には問題ありませんでした。起動が呼ばれました前に配置された使用しているので

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

、それが既に配置したとして何のSSはありませんでした:私は、createメソッドのコードのこれらの行を逃しました。これを投稿する前に少し深く見ていなければなりません:-(

関連する問題