2

I have been trying to connect to exchange online using Powershell. Here is a code I followed;

System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char c in AdminPass)
{
    securePassword.AppendChar(c);
}

PSCredential credential = new PSCredential(AdminUser, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://outlook.office365.com/powershell-liveid/"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.MaximumConnectionRedirectionCount = 2;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
     using (PowerShell powershell = PowerShell.Create())
     {
            runspace.Open(); //Its dying here
            powershell.Runspace = runspace;
            powershell.AddCommand("New-MailContact");
            powershell.AddParameter("Name", DisplayName);
            powershell.AddParameter("ExternalEmailAddress", UPN);
            Collection<PSObject> result = powershell.Invoke();
      }
}

As mentioned above, it's throwing error from line: runspace.Open()

Here is the error:

Connecting to remote server outlook.office365.com failed with the following error message : The parameter is incorrect. For more information, see the about_Remote_Troubleshooting Help topic.

Stack Trace:

   at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
   at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.EndOpen(IAsyncResult asyncResult)
   at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Open()
   at System.Management.Automation.RemoteRunspace.Open()
   at AzureAD.Helper.UserManagement.CreateContact(String DisplayName, String MailNickName, String Password, String UPN, String ImmutableId, String AdminUser, String AdminPass, String tenantName) in :line 263
   at AzureAD.Program.<CreateCompleteUsercn>d__3.MoveNext() in 

Any clues please?

Lisbeth
  • 129
  • 2
  • 11
Nitin Khubani
  • 354
  • 1
  • 4
  • 18
  • The code works well for me. The code works well for me. **AzureAD.Program.d__3.MoveNext() in** Are you writing the code in a loop? If yes, could the code run successfully at the first time? – Fei Xue May 31 '16 at 09:03
  • Hey, Ya its a loop. But it never works for me, not even first time.. – Nitin Khubani May 31 '16 at 12:30

1 Answers1

0

This code worked for me as well. If the AdminUser or AdminPassword were incorrect, you would get an Access Denied error, so that can't be the issue. I see this post is a couple years old, and I remember that after this endpoint (outlook.office365.com) was added it didn't work everywhere for a time. Is this working for you now? If not, perhaps try the old endpoint https://ps.outlook.com/powershell?

Alternatively, you could use Import-PSSession instead of WSMan to connect. See Exchange PowerShell commands through C# for more information on that.

Michael Tucker
  • 205
  • 2
  • 10