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;
}
}}