2009-04-23 7 views
1

Powershell 및 Exchange2007 cmdlet을 사용하여 Exchange에서 사용자 계정을 제공하는 Windows Forms 앱이 있습니다. 이 응용 프로그램에는 새 사용자에 대한 정보를 취한 다음 Powershell 명령을 실행하는 양식이 하나만 있습니다. 좋은 프로그래머와 마찬가지로 코드를 리팩토링하고 모든 Exchange 및 Active Directory 호출을 분리하여 별도의 클래스에 넣었습니다. 윈도우 형태에서, 나는 다음과 같은 버튼을 클릭 이벤트에 코드를 호출 클래스 자체에서외부 클래스 라이브러리 호출시 폐기 된 개체 오류에 액세스 할 수 없습니다.

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는 오류를 발생하고 내가 가진 처분되는 것이 전혀 모른다. 이 코드는 폼의 코드 숨김에있을 때 완벽하게 작동했습니다. 나 또한 별도의 클래스 라이브러리로 찢어 일부 액티브 디렉토리 방법을 가지고 그들은 잘 작동하는 것.

이 문제가 발생하지 않도록하려면 어떻게해야합니까? 감사!

답변

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가 없었다 배치 : 내가 만드는 방법에 코드 줄을 놓쳤다. 이 글을 게시하기 전에 조금 더 깊숙이 보였어야만합니다 :-(