【问题标题】:Contact not created when running New-MailContact cmdlet from C# code从 C# 代码运行 New-MailContact cmdlet 时未创建联系人
【发布时间】:2012-05-17 07:56:47
【问题描述】:

我有一个应用程序(除其他外)需要调用 New-MailContact cmdlet 并在 Active Directory 中创建联系人。我已经关注了一些技术文章以达到我所拥有的程度,但它仍然无法正常工作。

我已验证我正在使用的服务帐户具有基于 this TechNet page 的正确身份验证。我能够从 powershell 中找到并调用 cmdlet,并且没有收到任何错误。

但是,运行后我检查了我的 OU,但未创建我的联系人。我找到了this KB 文章,我认为这可能是可疑的,但由于 cmdlet 在调用后没有返回任何错误,我不能确定这会解决我的问题。

这是我正在做的事情的 sn-p:

    public bool CreateMailContactObject(ADExchangeContact adExchangeContacts)
    {
        Collection<PSObject> results;
        Pipeline pipeLine = null;

        try
        {
            var runspaceConfiguration = RunspaceConfiguration.Create();
            PSSnapInException snapInException;
            var snapInInfo = runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);

            using (var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                var newMailBoxContact = new Command("New-MailContact");
                newMailBoxContact.Parameters.Add("Name", adExchangeContacts.DisplayName);
                newMailBoxContact.Parameters.Add("ExternalEmailAddress", adExchangeContacts.ExternalEmailAddress);
                newMailBoxContact.Parameters.Add("OrganizationalUnit", adExchangeContacts.OrganizationalUnit);
                newMailBoxContact.Parameters.Add("Alias", adExchangeContacts.Alias);

                runspace.Open();

                pipeLine = runspace.CreatePipeline();
                pipeLine.Commands.Add(newMailBoxContact);

                results = pipeLine.Invoke();

                _log.DebugFormat("results.Count = {0}", results.Count);
                results.ForEach(x => x.Properties.ForEach(y => _log.DebugFormat("{0}: {1}", y.Name, y.Value)));

                pipeLine.Stop();
                runspace.Close();
            }

            return true;
        }
        catch (Exception ex)
        {
            // Add log statement
            _log.ErrorFormat("Creation of Mail Contact in AD Failed. Error: {0}", ex);
            return false;
        }            
    }

我没有得到任何异常,并且我的结果列表在管道调用中是空的。有什么我想念的吗?如果在 AD 中创建联系人时由于权限导致 cmdlet 失败,我不希望在来自pipeLine.Invoke() 的结果集中收到某种错误吗??

我是运行 Powershell 的新手,所以如果手头还有其他问题(知识库文章之外),请告诉我。

【问题讨论】:

    标签: c# powershell active-directory cmdlets


    【解决方案1】:
        if (pipeline.Error != null && pipeline.Error.Count > 0)
                    {
                        StringBuilder pipelineError = new StringBuilder();
                        pipelineError.AppendFormat("Error calling Add-MailboxPermission.");
                        foreach (object item in thisPipeline.Error.ReadToEnd())
                        {
                            pipelineError.AppendFormat("{0}\n", item.ToString());
                        }
    
                        ErrorText = ErrorText + "Error: " + pipelineError.ToString() + Environment.NewLine;
                    }
    

    请将此代码放在pipeline.Invoke()之后,并检查其中是否有错误

    更新: 我认为向用户授予正确权限是错误的,对此有一些解决方案:

    http://boardreader.com/thread/Microsoft_Exchange_2010_wont_allow_new_M_1w69j__37ad9f8a-cdcf-4d26-9384-00ad1a3d0f91.html
    http://blogs.technet.com/b/richardroddy/archive/2010/07/12/exchange-2010-and-the-exchange-trusted-subsystem.aspx

    【讨论】:

    • 谢谢。我将您的答案标记为正确。它并没有解决我的问题,但你教育了我如何从我的电话中获取更多细节。结果是:2012-05-08 15:58:38,227 [DEBUG] (MyProject.Core.Services.ADService) Error: Error calling New-MailContact.Active Directory operation failed on MY-SERVER.Domain.com. This error is not retriable. Additional information: Access is denied. Active directory response: 00000005: SecErr: DSID-031521D0, problem 4003 (INSUFF_ACCESS_RIGHTS), data 0 看来知识库文章是我的罪魁祸首。
    • 根据您的更新,我们使用的是 Exchange 2007,因此我认为这是我的服务帐户权限,而不是 ETS。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多