简体   繁体   English

使用C#进行Exchange Online Powershell连接

[英]Exchange Online Powershell connect using c#

I have been trying to connect to exchange online using Powershell. 我一直在尝试使用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 in above like, its throwing error from like: runspace.Open() 如上面提到的,它的抛出错误来自: runspace.Open()

Here is the error; 这是错误;

Connecting to remote server outlook.office365.com failed with the following error message : The parameter is incorrect. 连接到远程服务器outlook.office365.com失败,并显示以下错误消息:参数不正确。 For more information, see the about_Remote_Troubleshooting Help topic. 有关更多信息,请参见about_Remote_Troubleshooting帮助主题。

Stake 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? 有什么线索吗?

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. 如果AdminUser或AdminPassword不正确,则会出现“访问被拒绝”错误,因此不会成为问题。 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. 我看到这篇文章已有两年历史了,我记得在添加此端点(outlook.office365.com)之后,它并没有在所有时间都起作用。 Is this working for you now? 现在为您工作吗? If not, perhaps try the old endpoint https://ps.outlook.com/powershell ? 如果没有,也许尝试旧的端点https://ps.outlook.com/powershell

Alternatively, you could use Import-PSSession instead of WSMan to connect. 或者,您可以使用Import-PSSession而不是WSMan进行连接。 See Exchange PowerShell commands through C# for more information on that. 有关此方面的更多信息,请参见通过C#的Exchange PowerShell命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM