New-PSSession and Runspacepool clarification

梦想的初衷 提交于 2019-12-11 03:38:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!