I spent way too much time trying to use PowerShell WinRM to connect to Outlook Live this week. Here is my basic connection code:
public void ConnectToOutlookLive()
string username = "firstname.lastname@example.org";
string password = "plaintextpassword";
string uri = "https://ps.outlook.com/powershell/";
string schema = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
PSCredential psc = new PSCredential(username, securePassword);
WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);
rri.AuthenticationMechanism = AuthenticationMechanism.Basic;
//rri.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
Runspace remoteRunspace = RunspaceFactory.CreateRunspace(rri);
remoteRunspace.Open(); //Error happens when trying to open the connection
When using AuthenticationMechanism.Kerberos I received the following error:
System.Management.Automation.Remoting.PSRemotingTransportException was caught
Message=Connecting to remote server failed with the following error message : Logon failure: unknown user name or bad password. For more information, see the about_Remote_Troubleshooting Help topic.
TransportMessage=Logon failure: unknown user name or bad password.
When using AuthenticationMechanism.Basic I received a different error:
System.Management.Automation.Remoting.PSRemotingTransportRedirectException was caught
Message=The WinRM service cannot process the request because the request needs to be sent to a different machine. Use the redirect information to send the request to a new machine.
Exception when opening a Remote Runspace using WSManConnectionInfo – This was the only place on the whole internet I could find someone who was having the same problem… unfortunately the solution did not fix my issue.
The exception is being thrown because the remote server is redirecting you to a new location. So you have to set the MaximumConnectionRedirectionCount. Just add the following line of code before the open and everything works fine:
rri.MaximumConnectionRedirectionCount = 1;
I hope this saves someone a lot of time and frustration.