End Google Ads 201810 - BS.net 01 --> 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 *******ing 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 securePass = new SecureString(); foreach (char c in password.ToCharArray()) securePass.AppendChar(c); PSCredential psc = new PSCredential(username, securePass); WSManConnectionInfo wmci = new WSManConnectionInfo(new Uri(uri), schema, psc); wmci.AuthenticationMechanism = AuthenticationMechanism.Basic; remoteRunspace = RunspaceFactory.CreateRunspace(wmci); }
Here is the command running Enable-Mailbox:
public void Execute() { try { if (String.IsNullOrEmpty(Identity)) throw new Exception("Identity must be specified."); if (String.IsNullOrEmpty(Alias)) throw new Exception("Alias must be specified."); if (String.IsNullOrEmpty(DisplayName)) throw new Exception("Display Name must be specified."); if (String.IsNullOrEmpty(PrimarySmtpAddress)) throw new Exception("The Primary Smtp Address must be specified."); if (String.IsNullOrEmpty(CompanyCode)) throw new Exception("The Company Code must be specified."); // Set our command for Enable-Mailbox String enableMailboxCmd = String.Format(@"Enable-Mailbox -Identity '{0}' -AddressBookPolicy '{1} ABP' -Alias '{2}' -DisplayName '{3}' -PrimarySmtpAddress '{4}';", Identity, CompanyCode, Alias, DisplayName, PrimarySmtpAddress); // Set our command for Set-Mailbox String setMailbox = String.Format(@"Set-Mailbox -Identity '{0}' -OfflineAddressBook '{1} OAL' -CustomAttribute1 '{2}' -IssueWarningQuota:{3}KB -ProhibitSendQuota:{4}KB -ProhibitSendReceiveQuota:{5}KB -UseDatabaseQuotaDefaults $False;", Identity, CompanyCode, CompanyCode, WarningQuota, ProhibitSendQuota, ProhibitSendReceiveQuota); // Set our command for Set-CASMailbox String setCasMailbox = String.Format(@"Set-CASMailbox -Identity '{0}' -ActiveSyncEnabled:${1} -ActiveSyncMailboxPolicy '{2}' -IMAPEnabled:${3} -POPEnabled:${4} -MAPIEnabled:${5} -OWAEnabled:${6} -OWAMailboxPolicy '{7}';", Identity, ActiveSyncEnabled.ToString(), ActiveSyncPolicy, IMAPEnabled.ToString(), POPEnabled.ToString(), MAPIEnabled.ToString(), OWAEnabled.ToString(), OWAPolicy); this.RunPowershell(null, enableMailboxCmd + setMailbox + setCasMailbox); } catch (Exception ex) { throw ex; } finally { this.Close(); } }
Thank you for your help