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는 오류를 발생하고 내가 가진 처분되는 것이 전혀 모른다. 이 코드는 폼의 코드 숨김에있을 때 완벽하게 작동했습니다. 나 또한 별도의 클래스 라이브러리로 찢어 일부 액티브 디렉토리 방법을 가지고 그들은 잘 작동하는 것.
이 문제가 발생하지 않도록하려면 어떻게해야합니까? 감사!
그래,이게 무슨 일이야. 또 다른 기능이 없어지는 것 같아. – JasonMArcher