Hello,
Sorry if I am describing something wrong, but it is first time I am using Microsoft development forum.
I am trying to built library that will manage Exchange remotely using C# code (VS 2012) and Power-Shell ver.4.0. My workstation and the actual Exchange server physically placed in different domains. In order to test functionalities I am using user that is member of all administrating and management groups in the appropriative domain. Remote Power-Shell is enabled on Exchange.
In order to connect to the Service I am using that part of code :
internal static bool RedirectionUrlValidationCallback(string redirectionUrl) { bool result = false; var redirectionUri = new Uri(redirectionUrl); if (redirectionUri.Scheme.Equals("https")) { result = true; } return result; } internal static ExchangeService ConnectToService(ExchangeVersion version, string userName, string emailAddress, string password, string domainName, ref Uri autodiscoverUrl) { ExchangeService service; try { service = new ExchangeService(version); service.Credentials = new WebCredentials(userName, password, domainName); if (autodiscoverUrl.IsNull()) { var ads = new AutodiscoverService(); // Enable tracing. ads.TraceEnabled = true; // Set the credentials. ads.Credentials = service.Credentials; // Prevent the AutodiscoverService from looking in the local Active Directory // for the Exchange Web Services Services SCP. ads.EnableScpLookup = false; // Specify a redirection URL validation //callback that returns true for valid URLs. ads.RedirectionUrlValidationCallback = RedirectionUrlValidationCallback; // Get the Exchange Web Services URL for the user’s mailbox. var response = ads.GetUserSettings(emailAddress, UserSettingName.InternalEwsUrl); // Extract the Exchange Web Services URL from the response. autodiscoverUrl = new Uri(response.Settings[UserSettingName.InternalEwsUrl].ToString()); // Update Service Url service.Url = autodiscoverUrl; } else { service.Url = autodiscoverUrl; } } catch (FormatException fe) // The user principal name(email) is in a bad format. UPN format is needed. { LoggingFactory.LogException(fe); return null; } catch (AutodiscoverLocalException ale) { LoggingFactory.LogException(ale); return null; } catch (Exception e) { LoggingFactory.LogException(e); return null; } return service; }
Then I am trying to reach existing mailbox of that user, using this part of code :
try { var runSpace = getRunspace(); var pwShell = PowerShell.Create(); pwShell.Runspace = runSpace; var command = new PSCommand(); command.AddCommand("Get-Mailbox"); command.AddParameter("Identity", userPrincipalName); command.AddParameter("RecipientTypeDetails", recipientType); pwShell.Commands = command; var result = pwShell.Invoke(); if (result.IsNotNull() && result.Any()) { // TODO : fill appropriative DTO and return it. } else { // TODO : send back appropriative error. } closeRunSpace(runSpace); } catch (Exception e) { // TODO : log the exception and send back appropriative error. } private Runspace getRunspace() { var runSpaceConfig = RunspaceConfiguration.Create(); var runSpace = RunspaceFactory.CreateRunspace(runSpaceConfig); PSSnapInException snapEx; var psInfo = runSpaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx); runSpace.Open(); return runSpace; } private void closeRunSpace(Runspace runSpace) { runSpace.Close(); runSpace.Dispose(); }
With this part I had problem. I received exception that appropriative Snap-in is not installed on my computer. I had no installation disk of Exchange in order to install Management Tool (as I understand this is the way to add needed part) so I changed run space activation to that part of code :
private Runspace getRunspace() { var newCredentials = (PSCredential)null; var connectionInfo = new WSManConnectionInfo(new Uri("http://exchange.domainname.local/powershell?serializationLevel=Full"),"http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCredentials); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; var runSpace = RunspaceFactory.CreateRunspace(connectionInfo); runSpace.Open(); return runSpace; }
Now I am receiving exception :
"Connecting to remote server exchange.domainname.local failed with the following error message :
[ClientAccessServer=exchange,BackEndServer=exchange.domainname.local,RequestId=....,TimeStamp=...]
Access is denied. For more information, see the about_Remote_Troubleshooting Help topic."
Please help me to understand what I am doing wrong.
Thank you.
Alexander.