2

我需要使用 C# 中的 powershell 类运行 Exchange online cmdlet。

要运行交换在线 cmdlet,我需要建立一个远程 powershell 会话。

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

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

4

1 回答 1

0

您无需为池中的每个运行空间手动创建远程会话。相反,在使用以下重载实例化 runspacepool 时提供连接信息:(RunspaceFactory.CreateRunspacePool(Int32, Int32, RunspaceConnectionInfo)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);
于 2015-08-11T08:32:43.877 回答