在C#WPF中保持powershell运行空间的活力?

在C#WPF中保持powershell运行空间的活力?,c#,wpf,powershell,runspace,C#,Wpf,Powershell,Runspace,我计划使用C#talkingpowershell到DC和Exchange服务器构建一个Active Directory/Exchange管理控制台 我想在应用程序启动时建立到这些服务器的powershell连接,然后让它们保持活动状态,这样我就可以继续运行查询或脚本或其他任何东西,因为建立远程连接需要几秒钟的时间,而在您所做的每件事上都有这样的延迟是行不通的 我目前正在测试一个本地powershell运行空间,但每次我向它发送命令时,它都会关闭,并且在执行初始命令后无法重用它 如何防止运行空间关

我计划使用
C#
talking
powershell
DC
和Exchange服务器构建一个
Active Directory/Exchange
管理控制台

我想在应用程序启动时建立到这些服务器的
powershell
连接,然后让它们保持活动状态,这样我就可以继续运行查询或脚本或其他任何东西,因为建立远程连接需要几秒钟的时间,而在您所做的每件事上都有这样的延迟是行不通的

我目前正在测试一个本地
powershell
运行空间,但每次我向它发送命令时,它都会关闭,并且在执行初始命令后无法重用它

如何防止运行空间关闭,以便反复使用

编辑:代码 非常基本,只是创建一个运行空间,计划以后在基本功能完成后能够包含模块。其想法是创建一个运行空间,在调用执行powershell代码的函数时,将该运行空间分配给另一个变量,以便我可以重用它,但我可能很愚蠢。目前,我只有一个虚拟的“获取过程”,它是在单击按钮时发送的,还有一个显示输出的文本框

public partial class MainWindow : Window
{
    Runspace powerShellRunspace = RunspaceFactory.CreateRunspace();
    public MainWindow()
    {

        InitializeComponent();
        powerShellRunspace.Open();
        string[] modules;
        scriptOutput.Text = "test";
        modules = new string[5];
        modules[0] = "john";



        //string result = powerShellRun("Get-Process");
        //powerShellInitialize(modules);

    }

    public static void powerShellInitialize(string[] modules)
    {
        Runspace powerShellRunspace = RunspaceFactory.CreateRunspace();
        powerShellRunspace.Open();

    }

    public string powerShellRun(string commands, Runspace powerShellRunspace)
    {

        Runspace powerShellRunspace2 = powerShellRunspace;
        Pipeline powerShellPipeline = powerShellRunspace2.CreatePipeline();
        powerShellPipeline.Commands.Add(commands);
        Collection<PSObject> powerShellResult = powerShellPipeline.Invoke();
        //string result="temp";
        //return result;
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in powerShellResult)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        return stringBuilder.ToString();

    }
}
公共部分类主窗口:窗口
{
Runspace powerShellRunspace=RunspaceFactory.CreateRunspace();
公共主窗口()
{
初始化组件();
powerShellRunspace.Open();
字符串[]模块;
scriptOutput.Text=“测试”;
模块=新字符串[5];
模块[0]=“john”;
//字符串结果=powerShellRun(“获取进程”);
//powerShellInitialize(模块);
}
公共静态无效powerShellInitialize(字符串[]模块)
{
Runspace powerShellRunspace=RunspaceFactory.CreateRunspace();
powerShellRunspace.Open();
}
公共字符串powerShellRun(字符串命令、运行空间powerShellRunspace)
{
运行空间powerShellRunspace2=powerShellRunspace;
管道powerShellPipeline=powerShellRunspace2.CreatePipeline();
powerShellPipeline.Commands.Add(命令);
集合powerShellResult=powerShellPipeline.Invoke();
//字符串result=“temp”;
//返回结果;
StringBuilder StringBuilder=新的StringBuilder();
foreach(powerShellResult中的PSObject对象)
{
stringBuilder.AppendLine(obj.ToString());
}
返回stringBuilder.ToString();
}
}

此问题已在上得到回答

总之,您可以保持运行空间处于打开状态,但对于每个独立查询,您需要创建一个新的Powershell实例

例如:

Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();

//First Query
var firstQuery = PowerShell.Create();
firstQuery.Runspace = runspace;
firstQuery.AddScript("Write-Host 'hello'")

//Second Query
var secondQuery = PowerShell.Create();
secondQuery.Runspace = runspace;
secondQuery.AddScript("Write-Host 'world'")

您当前的代码是什么样子的?