hi guys, i'm running a recordset read as below and within that running commands against a 2010 exchange server. i wanted to know what you thnk the fastest method there is of doing this?Is the best mehod to put the connection to the exchange server outside the rdr.read, and then only have the runspace/command in the rdr.read? let me know what you think i'd really appreciate any input, as you can see i'm not expert!

while (rdr.Read())
                {
if (exchversion == "2010")
                    {
                        SecureString password = new SecureString();
                        string str_password = exchpwd;
                        string username = exchuser;
                        string liveIdconnectionUri = "http://"+exchserver+"/Powershell?serializationLevel=Full";
                        foreach (char x in str_password)
                        {
                            password.AppendChar(x);
                        }

                        PSCredential credential = new PSCredential(username, password);
                        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credential); 
                        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; 
                        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo); 
                        PowerShell powershell = PowerShell.Create();
                        PSCommand command = new PSCommand();
                        command.AddCommand("Set-Mailbox"); 
                        command.AddParameter("Identity", strsmtp); 
                        command.AddParameter("Type",type);
                        powershell.Commands = command;
                        ErrLog("NOTE:Running Set-Mailbox -Identity +"+strsmtp+" -Type "+type);
                        try 
                        { 
                            runspace.Open();
                            powershell.Runspace = runspace;
                            powershell.Invoke();
                            ErrLog("SUCCESS:Completed Successfully User " + strsmtp);
                        } 
                        catch (Exception ex) 
                        { 
                            ErrLog("ERROR: On User: "+strsmtp+" "+ ex.Message); 
                        } 
                        finally 
                        { 
                            runspace.Dispose(); 
                            runspace = null; 
                            powershell.Dispose(); 
                            powershell = null; 
                        } 
                    }}

Invoking PowerShell for each read could slow things down considerably, depending on the recordset size. Is it possible to move the recordset read and subsequent code inside a PowerShell script? I see credentials info being as a possible issue with this approach. There are ways however of securing any credentials in the script or via utilities as PShellExec.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.