0
我使用下面的代碼連接到office365和使用c#遠程PowerShell收回郵箱信息。連接到遠程服務器ps.outlook.com失敗
strExchange2010PSURI = "https://ps.outlook.com/PowerShell-LiveID?PSVersion=3.0";
strAccountName = "[email protected]";
strAccountPwd = "ppp";
public DataTable GetMailboxes(string searchMailbox)
{
DataTable dt = null;
List<string> mailboxes = new List<string>();
Command psCmd1 = new Command("Get-Mailbox");
psCmd1.Parameters.Add(new CommandParameter("Identity", "*" + searchMailbox + "*"));
Collection<PSObject> psExchMailboxInfo = fnGetPSData(psCmd1, null);
if (psExchMailboxInfo != null && psExchMailboxInfo.Count > 0)
{
//logic to get the mailbox details in a datatable
dt = GetMailboxInfo(mailboxes);
}
return dt;
}
private Collection<PSObject> fnGetPSData(Command psCmd1, Command psCmd2)
{
Pipeline psPipeLine = null;
Runspace psRunSpace = null;
WSManConnectionInfo psConnInfo = null;
var varSecurePwd = new SecureString();
try
{
foreach (var c in strAccountPwd)
{
varSecurePwd.AppendChar(c);
}
PSCredential psCreds = new PSCredential(strAccountName, varSecurePwd);
psConnInfo = new WSManConnectionInfo(new Uri(strExchange2010PSURI), "Microsoft.Exchange", psCreds);
psConnInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
psRunSpace = RunspaceFactory.CreateRunspace(psConnInfo);
psRunSpace.Open();
psPipeLine = psRunSpace.CreatePipeline();
if (psCmd1 != null)
{
psPipeLine.Commands.Add(psCmd1);
}
if (psCmd2 != null)
{
psPipeLine.Commands.Add(psCmd2);
}
Collection<PSObject> psObjects = psPipeLine.Invoke();
return psObjects;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (psPipeLine != null && psPipeLine.Commands != null)
{
psPipeLine.Commands.Clear();
psPipeLine.Dispose();
}
if (psRunSpace != null)
{
psRunSpace.Close();
psRunSpace.Dispose();
}
}
}
它工作在我的開發環境,但是當我嘗試同樣的生產,我得到的「連接到遠程服務器ps.outlook.com與以下錯誤消息失敗:訪問被拒絕有關詳細信息,請參閱about_Remote_Troubleshooting幫助主題「。
任何人都可以幫忙嗎?
督促服務器和網絡之間的防火牆?畢竟服務器不能直接訪問互聯網是很常見的 – BugFinder