我正在使用以下代码段在域服务器上创建一个新的AD用户:
DirectoryEntry newUser = directoryEntry.Children.Add("CN=" + model.Account.FullName, "user");
if (model.Account.SamAccountName != null) newUser.Properties["sAMAccountName"].Value = model.Account.SamAccountName;
newUser.CommitChanges();
setUserPassword("CN=" + model.Account.FullName + "," + model.Account.Path, model.Account.Password);
newUser.RefreshCache();
if (model.Account.FirstName != null) newUser.Properties["givenName"].Add(model.Account.FirstName);
if (model.Account.LastName != null) newUser.Properties["sn"].Add(model.Account.LastName);
if (model.Account.MiddleName != null) newUser.Properties["initials"].Add(model.Account.MiddleName);
if (model.Account.UPNLogon != null && model.Account.DomainName != null) newUser.Properties["userPrincipalName"].Add(model.Account.UPNLogon + "@" + model.Account.DomainName);
if (model.Organization.DisplayName != null) newUser.Properties["displayName"].Add(model.Organization.DisplayName);
if (model.Organization.Email != null) newUser.Properties["mail"].Add(model.Organization.Email);
newUser.Properties["LockOutTime"].Value = 0; //unlock account
newUser.Properties["userAccountControl"].Value = 0x200; // enable account
newUser.CommitChanges();
string homeMDB = profile.Exchange13_Profile.ExchangeDB;
IMailboxStore mailbox;
try
{
IMailboxStore mailbox = (IMailboxStore)NewUser;
mailbox.CreateMailbox(sHomeMDB);
NewUser.CommitChanges();
}
catch (InvalidCastException e)
{
MessageBox.Show(e.Message.ToString());
}
上面的代码成功创建了一个新用户,并在AD服务器上启用了该用户。但是由于IMailboxStore
命名空间的需要,我无法创建/启用Exchange邮箱cdoexm.dll
。我试着cdoexm.dll
在域控制器,邮箱服务器和客户端访问服务器上定位,但是很简单。
我知道执行此操作的另一种方法是使用Powershell cmdlet,但我不想这样做。
现在精确地陈述我的问题:
cdoexm.dll
?或者IMailBoxStore
吗?或者由于CDOEXM从Exchange 2010开始不再使用,因此前两个问题已得到解决。
从Exchange 2007开始删除了CDOEXM。这不是程序集,而是COM。
经过一些搜索,我只能使用PowerShell查找解决方案,
或者使用c#调用PowerShell。
要使用C#调用PowerShell:
添加“用法”:
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;
调用cmdlet:
PSCredential newCred = (PSCredential)null;
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeserver01.my.domain/powershell?serializationLevel=Full"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Enable-Mailbox");
command.AddParameter("Identity", user.Guid.ToString());
command.AddParameter("Alias", user.UserName);
command.AddParameter("DomainController", ConnectingServer);
powershell.Commands = command;
try
{
runspace.Open();
powershell.Runspace = runspace;
Collection<psobject> results = powershell.Invoke();
}
catch (Exception ex)
{
string er = ex.InnerException.ToString();
}
finally
{
runspace.Dispose();
runspace = null;
powershell.Dispose();
powershell = null;
}
以上内容是从以下链接(未经测试)复制而来的:
http://codingchris.com/2012/02/15/creating-exchange-2010-mailboxes-in-c/ http://codingchris.com/2014/02/11/creating-exchange-2013-mailbox-in- C/
但是,如果您想使用PowerShell以外的解决方案,可能上述方法没有用...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句