New-PSSession and Runspacepool clarification

本文关键字:clarification Runspacepool and New-PSSession | 更新日期: 2023-09-27 18:07:20

我需要在c#中使用powershell类运行Exchange在线cmdlet。

要运行exchange online cmdlets,我需要建立一个远程powershell会话。

我的疑问是:1)如果runspacepool的大小是2,我应该创建远程powershell会话在两个runspacepool的运行空间?如果是,我如何/是否有一种方法可以循环遍历运行空间以在两个运行空间中运行New-PSSession命令。

2)如果会话在一个运行空间过期,是否有办法从运行空间池中获得特定的运行空间,并单独创建新的会话?

New-PSSession and Runspacepool clarification

您不需要为池中的每个运行空间手动创建远程会话。相反,在使用以下过载实例化runspacepool时提供连接信息:RunspaceFactory.CreateRunspacePool(Int32, Int32, RunspaceConnectionInfo)(如此回答所示):

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