2

I need to run Exchange online cmdlets using powershell classes in C#.

To run exchange online cmdlets i need to establish a remote powershell session.

My doubts are: 1) If runspacepool size is 2, should i create that remote powershell session in both the runspaces in that runspacepool? If yes, how can I / Is there a way to loop through the runspaces to run the New-PSSession command in both the runspaces.

2) If session expires in ONE runspace, is there a way to get that particular runspace from the runspacepool, and create new session that runspace alone?

Praveen Kumar
  • 946
  • 3
  • 10
  • 29

1 Answers1

0

You don't need to manually create remote sessions for each runspace in the pool. Instead, supply connection information when instantiating the runspacepool with the following overload: RunspaceFactory.CreateRunspacePool(Int32, Int32, RunspaceConnectionInfo) (as shown in this answer):

string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
var target = new Uri("http://myserver/wsman");
var secured = new SecureString();
foreach (char letter in "mypassword")
{
    secured.AppendChar(letter);
}
secured.MakeReadOnly();

var credential = new PSCredential("username", secured);
var connectionInfo = new WSManConnectionInfo(target, shell, credential);

Runspace remotePool = RunspaceFactory.CreateRunspacePool(0,2,connectionInfo);
Community
  • 1
  • 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Hi Mathias R. Jessen, Consider using that runspacepool, i run exchange online cmdlets. But i dont have control which runspace in that runspacepool will run that cmdlet. There is a chance that remote pssession will expire in one runspace alone? at that time i should abandon this runspacepool and create a new runspacepool? (bcoz i cant run cmdlet to create new pssession in the session expired particular runspace) – Praveen Kumar Aug 11 '15 at 09:52
  • @PraveenKumar I don't think you need to worry about that. Btw, the shell schema would be `http://schemas.microsoft.com/powershell/Microsoft.Exchange` for Exchange Online – Mathias R. Jessen Aug 11 '15 at 10:03
  • you mean to say, session will not expire at all (or) even if session expires, it will be established automatically? – Praveen Kumar Aug 11 '15 at 10:16