无法使用C#中的不同帐户同时执行两个Office 365命令 [英] Unable to execute two Office 365 commands simultaneously using different accounts in C#

查看:82
本文介绍了无法使用C#中的不同帐户同时执行两个Office 365命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#同时查询两个不同Office 365帐户的用户.在两个Powershell Windows上进行尝试时,或者通过代码连接一个帐户并让另一个帐户获得用户时,此方法都可以正常工作.但是在同时使用代码时无法正常工作.

I am trying to query the users of two different Office 365 accounts simultaneously using C#. It works fine when trying from two Powershell Windows or when connecting and getting users one account after other by code. But not working when doing simultaneously using code.

在检查时,我发现从C#尝试时仅生成一个日志文件.但是,从PowerShell窗口尝试时会生成两个不同的日志文件.

On checking I found that only one log file is generated when trying from C#. But two different log files are generated when trying from PowerShell window.

日志文件夹位置:%userprofile%\ appdata \ Local \ Microsoft \ Office365 \ Powershell

Log folder location: %userprofile%\appdata\Local\Microsoft\Office365\Powershell

这意味着从代码运行时,它的工作原理就像在单个PowerShell窗口中运行,甚至在两个运行空间中一样.

Which implies that when running from code, it works like running with single PowerShell window even with two runspaces.

主要方法代码:

Thread t1 = new Thread(() => connectandExec("admin@domain1.onmicrosoft.com", "Pwdd@123"));
Thread t2 = new Thread(() => connectandExec("admin@domain2.onmicrosoft.com", "Pwdd@123"));
t1.Start();
t2.Start();

连接并获取用户的方法:

Method that connects and gets the user:

public static void connectandExec(String userName, String password) {
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new String[] { "MSOnline" });
Runspace runspace = RunspaceFactory.CreateRunspace(iss);
runspace.Open();

PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;

Command cmd = new Command("Connect-MsolService");
System.Security.SecureString pwd = new System.Security.SecureString();
foreach (Char c in password.ToCharArray()) {
pwd.AppendChar(c);
}
log("Connecting to : " + userName);
PSCredential pscred = new PSCredential(userName, pwd);
cmd.Parameters.Add("Credential", pscred);
ps.Commands.AddCommand(cmd);
ps.Invoke();
if (ps.Streams.Error.Count > 0) {
log("Error when connecting: " + userName);
foreach (ErrorRecord errRecord in ps.Streams.Error) {
log(userName + errRecord.ToString());
}
} else {
log("Connected to : " + userName);
}

ps.Commands.Clear();
try {
ps.Commands.AddScript("Get-MsolUser -All");
ICollection<PSObject> results = ps.Invoke();
if (ps.Streams.Error.Count > 0) {
log("Error when getting users: " + userName);
foreach (ErrorRecord errRecord in ps.Streams.Error) {
log(userName + errRecord.ToString());
}
} else {
foreach (PSObject obj in results) {
if (obj != null && obj.ToString() != "") {
Object val = obj.Members["UserPrincipalName"].Value;
if (val != null) {
log(userName + ":" + val.ToString());
}
}
}
}
} catch (Exception ex) {
log(userName + ":Exception during getUsers: " + ex.ToString());
}
}

推荐答案

您的代码正在尝试使用线程来执行应在不同的

Your code is trying to use threads to do something which should be done in different application domains: "An application domain forms an isolation boundary for security".

毫无疑问,Office 365库将使用当前线程的应用程序域-您仅使用了属于同一应用程序域的两个线程,因此会造成混乱/失败.

The Office 365 library will no doubt use the app domain of the current thread - and you're just using two threads which belong to the same app domain, hence the confusion / failure.

这篇关于无法使用C#中的不同帐户同时执行两个Office 365命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆