【问题标题】:Exchange Online Powershell connect using c#使用 c# 进行 Exchange Online Powershell 连接
【发布时间】:2016-09-27 22:59:02
【问题描述】:

我一直在尝试使用 Powershell 连接到在线交换。这是我遵循的代码;

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();
      }
}

如上所述,它从以下行抛出错误:runspace.Open()

这是错误:

连接到远程服务器outlook.office365.com 失败并显示以下错误消息:参数不正确。更多 信息,请参阅 about_Remote_Troubleshooting 帮助主题。

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 

请问有什么线索吗?

【问题讨论】:

  • 该代码对我来说效果很好。该代码对我来说效果很好。 AzureAD.Program.d__3.MoveNext() in 你是在循环写代码吗?如果是,代码能否在第一时间成功运行?
  • 嘿,它是一个循环。但这对我来说从来没有用过,即使是第一次也不行..

标签: c# powershell office365


【解决方案1】:

这段代码也适用于我。如果 AdminUser 或 AdminPassword 不正确,您将收到拒绝访问错误,因此这不是问题。我看到这篇文章已经有几年的历史了,我记得在添加了这个端点(outlook.office365.com)之后,它一度无法在任何地方工作。这对你有用吗?如果没有,也许试试旧端点https://ps.outlook.com/powershell

或者,您可以使用 Import-PSSession 代替 WSMan 进行连接。有关详细信息,请参阅Exchange PowerShell commands through C#

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 2020-11-24
    • 2021-06-09
    • 1970-01-01
    • 2020-10-06
    • 2022-09-29
    • 2021-07-14
    • 2016-07-14
    • 1970-01-01
    相关资源
    最近更新 更多