Unable to execute two Office 365 commands simultan

2019-06-08 03:12发布

问题:

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.

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.

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

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

Main method code:

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

回答1:

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".

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.