Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:09:52+00:00 2026-06-04T17:09:52+00:00

I’m trying to run some PowerShell code from within VB.Net (C# coders may also

  • 0

I’m trying to run some PowerShell code from within VB.Net (C# coders may also help if you know!).
The first part of the code, I need to connect to a na-controller with a password and I need to keep the connection open. I have other commands I need to run with a click of a button (get files, show files, delete specific ones).
How can I keep the runspace open and run each line of code separately? Do I need to use the “CreateRunspacePool”? If so, how do I use it?

Edit: I’m playing around now with the RunspaceStateInfo.State, but I don’t know of a way to keep the connection open. I’ve tried the Page_Load and the Page_Init events to just have it open once, but somewhere along the line, it closes. I did remove the “.Close” command

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:09:53+00:00Added an answer on June 4, 2026 at 5:09 pm

    You have not provided a lot of info but from you talking about the Page_Load and Page_Init I am assuming you are trying to do this from an ASP.NET web application. By pressing buttons and setting state on the Runspace.

    If this is a winforms application you can simply create a runspace:

    Runspace runspace = RunspaceFactory.CreateRunspace(Host);
    

    And then create powershell instances and just reuse this runspace:

    var ps1 = PowerShell.Create();
    ps1.Runspace = runspace;
    ps1.AddScript("Write-Host 'hello'")
    
    var ps2 = PowerShell.Create();
    ps2.Runspace = runspace;
    ps2.AddScript("Write-Host 'world'")
    

    You can keep the runspace around and simply run scripts between button clicks against that same runspace.

    If you are in asp.net however this is different, obviously each time you click a button a new thread is spawned and you will not be able to keep the runspace in a variable, so you would need to do something like keep it around in the session as follows:

    protected PSHost Host
    {
        get
        {
    
            if (this.Session["Host"] == null)
            {
                var host = new MyHost();
                this.Session["Host"] = host;
            }
    
            return (PSHost)this.Session["Host"];
        }
    }
    
    
    protected Runspace Runspace
    {
        get
        {
    
            if (this.Session["Runspace"] == null)
            {
                var rs = RunspaceFactory.CreateRunspace(Host);
                this.Session["Runspace"] = rs;
                rs.Open();
            }
    
            return (Runspace)this.Session["Runspace"];
        }
    }
    

    And then I tested that this works:

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += new EventHandler(Button1_Click);
        Button2.Click += new EventHandler(Button2_Click);
        Button3.Click += new EventHandler(Button3_Click);
    }
    
    void Button3_Click(object sender, EventArgs e)
    {
        var ps = PowerShell.Create();
        ps.Runspace = this.Runspace;
        ps.AddScript("$test | ft | out-string");
        var input = new List<object>();
        var output = new List<object>();
        ps.Invoke(input, output);
        TextBox1.Text = output.First().ToString();
    }
    
    void Button2_Click(object sender, EventArgs e)
    {
        var ps = PowerShell.Create();
        ps.Runspace = this.Runspace;
        ps.AddScript("$test = 'world'");
        ps.Invoke();
    }
    
    void Button1_Click(object sender, EventArgs e)
    {
        var ps = PowerShell.Create();
        ps.Runspace = this.Runspace;
        ps.AddScript("$test = 'hello'");
        ps.Invoke();
    }
    

    When I click button 1 and then 3 it displays “Hello” and

    When I click button 2 and then 3 it displays “World”

    So it sucesfully reused the runspace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.