繁体   English   中英

.net(C#)中的持久运行空间

[英]Persistent Runspace in .net (C#)

我想在运行Power Shell会话的某些网页代码中创建一个运行空间。我希望在用户打开页面时创建此运行空间,并保持打开状态直到关闭。 因此,例如下面的代码是用于一键单击的。 但每次我运行Power Shell命令时,我都需要运行

"$S = new-pssesession -configurationname micros......"

这需要花费几秒钟的时间,并且只是在设置会话。我可以在哪里创建运行空间和Powershell对象,以便可以从代码中的任何位置调用它,而不必每次都想重新创建它。 在控制台应用程序中,我会在“主”类中插入,但似乎网页/应用程序具有不同的结构

protected void Button1_Click(object sender, EventArgs e)
    {

        var runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        var powershell = PowerShell.Create();
        //{
        powershell.Runspace = runspace;


        powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://server.com/powershell -authentication kerberos ; import-pssession $s");
        powershell.Invoke();
        powershell.AddScript("get-dynamicdistributiongroup");
        Collection<PSObject> resultsL = powershell.Invoke();


        // close the runspace 
        runspace.Close();

        Input.Items.Clear();
        foreach (var dlist in resultsL)
        { 
            Input.Items.Add(dlist.ToString());
        }

        Input.DataBind();

我建议的解决方案

所以我想我将尝试组成一个静态对象,并可以对其进行调用。 我认为通过创建一个静态调用,我第一次调用它会运行“ static powers()”构造,但是任何其他调用只会调用该方法中的代码部分。 但是不确定我是否正确。

public static class powers
    {

        static public Runspace runspace = RunspaceFactory.CreateRunspace();
        static public PowerShell powershell = PowerShell.Create();

        static powers()
        {

            runspace.Open();
            powershell.Runspace = runspace;
            powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.com/powershell -authentication kerberos ; import-pssession $s");
            var resultL = powershell.Invoke();

        }

        static public Collection<PSObject> glist()
        {


            powershell.AddScript("get-dynamicdistributiongroup");
            Collection<PSObject> resultL = powershell.Invoke();
            return resultL;

        }

        static public Collection<PSObject> gmembers(string listn)
        {

            string GetMembers = "$FTE = Get-DynamicDistributionGroup '" + listn  + "'";
            powershell.AddScript(GetMembers);
            powershell.Invoke();
            powershell.AddScript("Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter");
            Collection<PSObject> resultM = powershell.Invoke();
            return resultM;

        }
    }

我建立了其他几个应用程序,在其中将事物存储在会话中,并使用属性来访问它们。 与此类似的内容可能对您有用。

public static Runspace runspace
{
  get
  {
    //if not exist, create, open and store
    if (Session["runspace"] == null){
        Runspace rs = RunspaceFactory.CreateRunspace();
        rs.Open();
        Session["runspace"] = rs;
    }

    //return from session
    return (Runspace)Session["runspace"];
  }
}

然后,您可以在事件处理程序中简单地将其作为静态属性进行访问。

暂无
暂无

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

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