Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
AnswerRe: Listbox Pin
BobJanova29-Apr-12 23:21
BobJanova29-Apr-12 23:21 
QuestionActiveX controls are not supported. Pin
Member 872600927-Apr-12 12:01
Member 872600927-Apr-12 12:01 
AnswerRe: ActiveX controls are not supported. Pin
VJ Reddy27-Apr-12 14:02
VJ Reddy27-Apr-12 14:02 
QuestionUsage of xmlserializer with two different objects Pin
Ruud23827-Apr-12 7:55
Ruud23827-Apr-12 7:55 
AnswerRe: Usage of xmlserializer with two different objects Pin
ekolis27-Apr-12 9:17
ekolis27-Apr-12 9:17 
GeneralRe: Usage of xmlserializer with two different objects Pin
Ruud23830-Apr-12 22:38
Ruud23830-Apr-12 22:38 
GeneralRe: Usage of xmlserializer with two different objects Pin
ekolis1-May-12 3:24
ekolis1-May-12 3:24 
QuestionExchange Powershell with C# Pin
JD8627-Apr-12 5:31
JD8627-Apr-12 5:31 
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:
C#
public void RunPowershell(PSCommand pscmd = null, String script = null)
        {
            Runspace remoteRunspace = null;
            PSDataCollection<ErrorRecord> errors = new PSDataCollection<ErrorRecord>();

            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:
C#
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
Questioninclude a file Pin
__John_27-Apr-12 4:37
__John_27-Apr-12 4:37 
AnswerRe: include a file Pin
__John_27-Apr-12 4:42
__John_27-Apr-12 4:42 
GeneralRe: include a file Pin
Dave Kreskowiak27-Apr-12 8:13
mveDave Kreskowiak27-Apr-12 8:13 
GeneralRe: include a file Pin
PIEBALDconsult27-Apr-12 8:29
mvePIEBALDconsult27-Apr-12 8:29 
JokeRe: include a file Pin
Big Daddy Farang27-Apr-12 10:05
Big Daddy Farang27-Apr-12 10:05 
AnswerRe: include a file Pin
VJ Reddy27-Apr-12 5:03
VJ Reddy27-Apr-12 5:03 
AnswerRe: include a file PinPopular
PIEBALDconsult27-Apr-12 5:32
mvePIEBALDconsult27-Apr-12 5:32 
GeneralRe: include a file Pin
jschell28-Apr-12 7:39
jschell28-Apr-12 7:39 
QuestionC# Media Library Help ! Pin
O.Calderbank27-Apr-12 2:27
O.Calderbank27-Apr-12 2:27 
AnswerRe: C# Media Library Help ! Pin
Richard MacCutchan27-Apr-12 2:42
mveRichard MacCutchan27-Apr-12 2:42 
AnswerRe: C# Media Library Help ! Pin
Wes Aday27-Apr-12 10:03
professionalWes Aday27-Apr-12 10:03 
QuestionDocument opening/saving idiom Pin
Orjan Westin27-Apr-12 1:07
professionalOrjan Westin27-Apr-12 1:07 
AnswerRe: Document opening/saving idiom PinPopular
Pete O'Hanlon27-Apr-12 1:57
mvePete O'Hanlon27-Apr-12 1:57 
AnswerRe: Document opening/saving idiom Pin
PIEBALDconsult27-Apr-12 3:22
mvePIEBALDconsult27-Apr-12 3:22 
AnswerRe: Document opening/saving idiom Pin
Eddy Vluggen27-Apr-12 7:04
professionalEddy Vluggen27-Apr-12 7:04 
AnswerRe: Document opening/saving idiom Pin
OriginalGriff27-Apr-12 9:29
mveOriginalGriff27-Apr-12 9:29 
NewsExpand the text in the RichTextBox. Pin
nqs_vn27-Apr-12 0:10
nqs_vn27-Apr-12 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.