Contact not created when running New-MailContact cmdlet from C# code
-
06-06-2021 - |
Question
I have an application which (among other things) needs to call the New-MailContact cmdlet and create contacts in Active Directory. I have followed a handful of tech articles to get as far as I have, but it is still not working.
I have verified my service account being used has the proper authentication based on this TechNet page. I am able to find and invoke the cmdlet from powershell, and I recieve no errors.
However, after running I inspect my OU and my contact was not created. I found this KB article which I think may be suspect, but since the cmdlet doesn't return any errors after the invoke, I can't be sure that this will solve my problem.
Here is a snippet of what I am doing:
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;
}
}
I do not get any exceptions, and my result list is empty from the Pipeline invoke. Is there something I am missing? If the cmdlet fails due to permissions when creating the contact in AD, wouldn't I expect to recieve some sort of error in the result set from pipeLine.Invoke()
??
I am new with running Powershell, so if there is another issue (beyond the KB article) that could be at hand, please let me know.
Solution
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;
}
Please Put this code after pipeline.Invoke() and check if there is any error in there
Update: I think this is erro with giving right permission to user,some solutions for this:
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