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