简体   繁体   English

以编程方式管理Powershell远程会话的最佳方法(服务器重新启动时引发InvalidRunspaceStateException-修复此问题的最佳方法?)

[英]Optimal way to manage powershell remote session programmatically (throwing InvalidRunspaceStateException when server restarts - best way to fix it?)

I am not sure how to ask the exact question, but let me give a try, hopefully incrementally i can refine :) 我不确定如何提出确切的问题,但是让我尝试一下,希望我可以逐步完善:)

I understand there is a limit on the number of powershell sessions that can be created (for ex: i tried a simple program to open powershell exchange remote sessions and it didnt allow me to create more 18 powershell sesssions. so i am assumings its a definite number) 我知道可以创建的Powershell会话数量是有限制的(例如:我尝试过一个简单的程序来打开Powershell交换远程会话,但是它不允许我创建更多18个Powershell会话。因此,我假设它是一个确定的数)

On the other hand, how many runspaces i can create and open? 另一方面,我可以创建和打开几个运行空间? in other words, can i simply create a new runspace whenever i tried to execute cmds - this way i dont need to worry about runsapce state as whenever i try to execute the cmd it always uses new runspace. 换句话说,只要我尝试执行cmd,我是否可以简单地创建一个新的运行空间-这样,我就不必担心runapce状态,因为每当我尝试执行cmd时,它总是使用新的运行空间。

Scenario: I have created a remote session (to be precise psremote session to a mailbox exchange server - programmatically in C# .net). 方案:我创建了一个远程会话(准确地说是到邮箱交换服务器的psremote会话-以编程方式在C#.net中)。 I have also created a runspace and using the runspace and powershell session i am executing the powershell cmds on the target server (ie; on my excahnge server - please see code snippet #1 to see how i am executing a script block on a remote server) 我还创建了一个运行空间,并使用运行空间和powershell会话在目标服务器上执行powershell cmd(即,在我的exchange服务器上-请参见代码段#1,以了解我如何在远程服务器上执行脚本块) )

Case: Assume that target server is restarted (ie; the mailbox server that i created remote session restarted) 案例:假设目标服务器已重新启动(即,我创建的远程会话的邮箱服务器已重新启动)

Issue: As soon as i restarted the server, the program throws that runspace is in invalid state (InvalidRunspaceStateException - http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.invalidrunspacestateexception(v=vs.85).aspx ) 问题:重新启动服务器后,程序会抛出运行空间处于无效状态的信息(InvalidRunspaceStateException- http://msdn.microsoft.com/zh-cn/library/system.management.automation.runspaces.invalidrunspacestateexception (v= vs.85).aspx

Question: What is the optimial way to fix it :)? 问题:修复它的最佳方法是什么?

I know one easy way to fix it is by catching the invalidstate exception and creating the runsapce and remote session again - but is this the ideal way? 我知道一种简单的解决方法是捕获无效状态异常,然后再次创建runsapce和远程会话-但这是理想的方法吗?

Is there any other optimal or nicer way to manage the session rather than catching the exception and creating runspace and the session again? 还有其他最佳或更好的方法来管理会话,而不是捕获异常并再次创建运行空间和会话吗?

    //(sample) CODE SNIPPET1 - EXECUTING PS CMDS ON REMOTE SERVER
    Runspace rs = RunspaceFactory.CreateRunspace();
    rs.Open();
    using (Pipeline pipeline = rs.CreatePipeline())
    {
        if (null == session)
        {
            pipeline.Commands.Add(command);
        }
        else
        {
            Command cmd = new Command("invoke-command");
            cmd.Parameters.Add("session", session);
            cmd.Parameters.Add("scriptblock", ScriptBlock.Create(command.ToString()));
            if (null != argumentList)
            {
                cmd.Parameters.Add("ArgumentList", argumentList);
            }
            pipeline.Commands.Add(cmd);
        }
        var results = pipeline.Invoke();
    }

Btw, after some debugging this is what i found: 顺便说一句,经过一些调试,这是我发现的:

Please see the attached image (the PSsession.Runspace.RunspaceInfo.State is broken and Pssession.runspace.runspaceinfo.reason is not null (has exception) 请查看所附图像(PSsession.Runspace.RunspaceInfo.State损坏,Pssession.runspace.runspaceinfo.reason不为null(有例外)

Probably, whenver i see the runspace of a pssession is not opened, i should just create a new pssession. 也许,每当我看到未打开pssession的运行空间时,我都应该创建一个新的pssession。

Thanks in advance, Regards, Dreamer 在此先感谢,问候,梦想家

As mentioned i just checked for the session's runspace state. 如前所述,我只是检查了会话的运行空间状态。 if it is broken, discarded the session created new one. 如果损坏,则丢弃该会话创建的新会话。

You can refer to code snippet in : Deadlock - How to know which thread has lock preferably using visual studio debugger (.net programming)? 您可以参考以下代码段: 死锁-最好使用Visual Studio调试器(.net编程),如何知道哪个线程已锁定?

Regards, Dreamer 问候,梦想家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM