【问题标题】:New-PSSession and Runspacepool clarificationNew-PSSession 和 Runspacepool 说明
【发布时间】:2015-11-03 06:57:21
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# powershell runspace


    【解决方案1】:

    您无需为池中的每个运行空间手动创建远程会话。相反,在使用以下重载实例化运行空间池时提供连接信息: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);
    

    【讨论】:

    • 嗨 Mathias R. Jessen,考虑使用那个 runspacepool,我运行在线交换 cmdlet。但我无法控制该运行空间池中的哪个运行空间将运行该 cmdlet。远程 pssession 是否有可能仅在一个运行空间中过期?那时我应该放弃这个runspacepool并创建一个新的runspacepool吗? (因为我无法运行 cmdlet 在会话过期的特定运行空间中创建新的 pssession)
    • @PraveenKumar 我认为您不必担心这一点。顺便说一句,对于 Exchange Online,shell 架构将是 http://schemas.microsoft.com/powershell/Microsoft.Exchange
    • 你的意思是说,session根本不会过期(或者)即使session过期了也会自动建立?
    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多