问题
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?
回答1:
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);
来源:https://stackoverflow.com/questions/31935183/new-pssession-and-runspacepool-clarification