Exchange Powershell with C#
-
I don't really have a big issue with the powershell working because it works just fine. The problem I'm having is the method exits before the command is actually completed on the Exchange server. I think it may have something to do with the way Exchange throttles powershell commands. Is there a way to use powershell with Exchange but have it completely wait until the command is finish before refreshing my page? Let me give you an example: A user logs into the web interface and decides to enable their user for Exchange. So the user clicks Enable Exchange. This fires a powershell to Exchange running the Enable-Mailbox command. After it completes the command it retrieves updated information from Active Directory / Exchange about the users mailbox. The problem I have is when it tries to retrieve the updated information it hasn't had time to populate in Exchange so it still thinks there isn't a mailbox. I know I could put a sleep in there but I rather do something cleaner. Here is my base class:
public void RunPowershell(PSCommand pscmd = null, String script = null)
{
Runspace remoteRunspace = null;
PSDataCollection errors = new PSDataCollection();OpenRunspace(\_uri, \_schema, \_username, \_password, ref remoteRunspace); remoteRunspace.Open(); powershell.Runspace = remoteRunspace; powershell.Streams.ClearStreams(); powershell.Commands.Clear(); if (pscmd != null) { String debugLog = "\[RunPowershell\] Executing powershell: "; foreach (Command cmd in pscmd.Commands) { debugLog += cmd.CommandText; foreach (CommandParameter cp in cmd.Parameters) debugLog += String.Format("-{0} {1}", cp.Name, cp.Value.ToString()); } Log.Debug(debugLog); powershell.Commands = pscmd; } else if (script != null) { Log.Debug("\[RunPowershell\] Executing powershell: " + script); powershell.Commands.AddScript(script); } powershell.Invoke(); ThrowIfError(); Log.Debug("\[Exited\] RunPowershell"); } private void OpenRunspace(string uri, string schema, string username, string password, ref Runspace remoteRunspace) { SecureString securePa