问题描述
我需要使用C#中的powershell类运行Exchange联机cmdlet.
I need to run Exchange online cmdlets using powershell classes in C#.
要运行在线交换cmdlet,我需要建立一个远程Powershell会话.
To run exchange online cmdlets i need to establish a remote powershell session.
我的怀疑是:1)如果runspacepool的大小为2,我应该在该runspacepool中的两个runspace中都创建该远程Powershell会话吗?如果是,我/有什么方法可以循环遍历Runspace以运行New在两个运行空间中都使用-PSSession命令.
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)如果会话在一个运行空间中到期,是否有办法从运行空间池中获取特定运行空间,并单独创建一个新的运行空间?
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?
推荐答案
您无需为池中的每个运行空间手动创建远程会话.而是在实例化具有以下重载的运行空间池时提供连接信息: RunspaceFactory.CreateRunspacePool(Int32, Int32, RunspaceConnectionInfo)
(如):
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);
这篇关于New-PSSession和Runspacepool的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!