0

尝试使用 C# 和 Exchange Shell 创建新邮箱时,我没有收到任何错误,但数据没有被推送到服务器。如果我确实犯了一个错误,比如在密码字段中输入一个字符串,我会收到一条错误消息。我也可以手动执行此过程,并且可以正常工作。我应该提供更多信息还是这个实现已经过时了。这是我目前正在使用的代码:

public Boolean CreateUserMailbox(string FirstName, string LastName, string Alias, string PassWord, string DomainName, string OrganizationalUnit)
        {
            string Name = FirstName + " " + LastName;
            string PrincipalName = Alias + "@" + DomainName;

            Boolean success = false;
            RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
            SecureString spassword = new SecureString();
            spassword.Clear();

            foreach (char c in PassWord)
            {
                spassword.AppendChar(c);
            }

            PSSnapInException snapInException = null;
            PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
            myRunSpace.Open();
            Pipeline pipeLine = myRunSpace.CreatePipeline();

            Command myCommand = new Command("New-MailBox");
            myCommand.Parameters.Add("Name", Name);
            myCommand.Parameters.Add("Alias", Alias);
            myCommand.Parameters.Add("UserPrincipalName", PrincipalName);
            myCommand.Parameters.Add("Confirm", false);
            myCommand.Parameters.Add("SamAccountName", Alias);
            myCommand.Parameters.Add("FirstName", FirstName);
            myCommand.Parameters.Add("LastName", LastName);
            myCommand.Parameters.Add("Password", "pass");
            myCommand.Parameters.Add("ResetPasswordOnNextLogon", false);
            myCommand.Parameters.Add("OrganizationalUnit", OrganizationalUnit);
            pipeLine.Commands.Add(myCommand);
            pipeLine.Commands.Add(myCommand);

            try
            {
                pipeLine.Invoke();
                Console.WriteLine("ERROR:", pipeLine.Error.ToString());


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            finally
            {
                myRunSpace.Dispose();
                success = true;
            }
            return success;
        } 
4

0 回答 0