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)

                        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.AddParameter("Identity", strsmtp); 
                        powershell.Commands = command;
                        ErrLog("NOTE:Running Set-Mailbox -Identity +"+strsmtp+" -Type "+type);
                            powershell.Runspace = runspace;
                            ErrLog("SUCCESS:Completed Successfully User " + strsmtp);
                        catch (Exception ex) 
                            ErrLog("ERROR: On User: "+strsmtp+" "+ ex.Message); 
                            runspace = null; 
                            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.

This article has been dead for over six months. Start a new discussion instead.