【发布时间】: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