Skip to content

Feature Request - Bring back ConsoleShell functionallity #9734

Description

@asrosent

Hey guys,
In my product team we created a custom powershell-based shell in as a way to controll our cloud service.
We did this based on the ConsoleShell object and customised RunspaceConfiguration.

As a part of moving to .Net Core we started examing the posibillity of moving our shell to powershell core but as we all know RunspaceConfiguration does not exist in the core version and there is no way to pass InitialSessionState to the ConsoleShell.Start() method.

if we could pass a initialSessionState to the Start method that would be great!

Thanks,
Asaf.

Activity

  1. added
    Issue-Enhancement
    on May 26, 2019
  2. iSazonov commented on May 27, 2019

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) Could you please comment the scenario and feature request? What is recommended way to create custom PowerShell console shell?

  3. SteveL-MSFT commented on May 29, 2019

    @SteveL-MSFT
    Member

    It seems that when we removed RunspaceConfiguration, we should have added back an overload to accept InitialSessionState. cc James Truher (@JamesWTruher)

  4. added
    WG-Engine
    on May 30, 2019
  5. ritchxu commented on Oct 22, 2019

    @ritchxu

    asrosent Steve Lee (@SteveL-MSFT) is ConsoleShell class included in Microsoft.PowerShell.SDK nuget package? I seem to have hard time finding it with version 6.2.3

  6. vexx32 commented on Oct 22, 2019

    @vexx32
    Collaborator

    As noted above by the bot, the change was made available in v7.0.0-preview.2 -- I expect a new SDK package will be published when 7.0.0 gets a GA release (which if I recall correctly may be sometime in December/January?)

    Don't think the PS team tend to publish preview SDKs, historically speaking. 🙂

  7. ritchxu commented on Oct 22, 2019

    @ritchxu

    Thanks Rain Sallow (/u/ta11ow) (@vexx32) for pointing that out, the related PR adds back taking InitialSessionState as parameter, but what I'm experiencing is I couldn't even find the ConsoleShell class. But before the PR, the class still exists publicly, so I was thinking if I'm looking at the wrong package...

  8. vexx32 commented on Oct 22, 2019

    @vexx32
    Collaborator

    Oh, that's a good point, my apologies! Not sure... have to defer to Steve Lee (@SteveL-MSFT) on that point. 🙂

  9. SteveL-MSFT commented on Oct 22, 2019

    @SteveL-MSFT
    Member

    The Microsoft.PowerShell.ConsoleShell class has always been there. The only change was to add back an overload:

    PS> [Microsoft.PowerShell.ConsoleShell]::Start
    
    OverloadDefinitions
    -------------------
    static int Start(string bannerText, string helpText, string[] args)
    static int Start(initialsessionstate initialSessionState, string bannerText, string helpText, string[] args)
  10. ritchxu commented on Oct 22, 2019

    @ritchxu

    Steve Lee (@SteveL-MSFT) that's what I thought, but I couldn't find it in PowerShell Core:
    https://docs.microsoft.com/en-us/dotnet/api/?view=pscore-6.2.0&term=ConsoleShell
    Is it part of the Microsoft.PowerShell.Sdk package?
    https://www.nuget.org/packages/Microsoft.PowerShell.SDK/

  11. vexx32 commented on Oct 22, 2019

    @vexx32
    Collaborator

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.consoleshell?view=powershellsdk-1.1.0

    Not sure why it's missing from that API browser thought... weird. Pretty sure it should be in the SDK? Might be a docs issue, potentially.

    This class provides an entry point which is called by minishell's main to transfer control to Msh console host implementation.
  12. 11 remaining items

  13. SteveL-MSFT commented on Oct 25, 2019

    @SteveL-MSFT
    Member

    Unfortunately, it seems there was a bug in 6.2 that has since been fixed in 7. I don't see any workaround to stay on 6. We are planning to GA PS7 in Jan, so it's not too far off.

  14. ritchxu commented on Nov 6, 2019

    @ritchxu

    Steve Lee (@SteveL-MSFT) with preview-5, I updated my sample code to

    using Microsoft.PowerShell;
    using System.Management.Automation.Runspaces;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                InitialSessionState config = InitialSessionState.CreateDefault();
                config.StartupScripts.Add("Get-ChildItem");
                ConsoleShell.Start(config, "", "", args);
            }
        }
    }
    

    It seems like StartupScripts are not executed?

  15. daxian-dbw commented on Jan 9, 2020

    @daxian-dbw
    Member

    Ray Xu (@ritchxu) StartupScripts are executed, but the results are not collected and not displayed. The code that executes it is here:

    try
    {
    psToInvoke.Invoke();
    }
    catch (Exception e)
    {
    if (ThrowOnRunspaceOpenError)
    {
    return e;
    }
    }

  16. iSazonov commented on Jan 10, 2020

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) Maybe add logging in the code or script block logging already works here?

  17. ritchxu commented on Jan 10, 2020

    @ritchxu

    Dongbo Wang (@daxian-dbw) here is my proof of concept code. I was expecting current working directory to become C:\ once the console shell is started. Did I miss anything?

    Program.cs:

    using Microsoft.PowerShell;
    using System.Management.Automation.Runspaces;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                InitialSessionState initialSessionState = InitialSessionState.CreateDefault2();
                initialSessionState.StartupScripts.Add(@"Set-Location C:\");
                ConsoleShell.Start("Hello", "", new string[0] { });
            }
        }
    }
    

    Test.csproj

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0-preview.6" />
      </ItemGroup>
    
      <ItemGroup>
        <Reference Include="Microsoft.PowerShell.ConsoleHost">
          <HintPath>$(NuGetPackageRoot)\microsoft.powershell.consolehost\7.0.0-preview.6\runtimes\win\lib\netcoreapp3.1\Microsoft.PowerShell.ConsoleHost.dll</HintPath>
        </Reference>
      </ItemGroup>
    
    </Project>
    
  18. daxian-dbw commented on Jan 10, 2020

    @daxian-dbw
    Member

    Ray Xu (@ritchxu) initialSessionState.StartupScripts is for you to run one more more script files (.ps1), not script code directly. Here is the code to process the startup scripts:

    private Exception ProcessStartupScripts(Runspace initializedRunspace)
    {
    foreach (string startupScript in StartupScripts)
    {
    using (PowerShell psToInvoke = PowerShell.Create())
    {
    psToInvoke.AddCommand(new Command(startupScript, false, false));
    Exception exceptionToReturn = ProcessPowerShellCommand(psToInvoke, initializedRunspace);
    if (exceptionToReturn != null)
    {
    // ThrowOnRunspaceOpenError handling is done by ProcessPowerShellCommand
    return exceptionToReturn;
    }
    }
    }
    return null;
    }

    This line psToInvoke.AddCommand(new Command(startupScript, false, false)); creates a new command by specifying that it's not a script (the first false argument).

    The name of that property is unfortunately vague 😦 The following is an example to use this property:

    Program.cs:

    using System;
    using System.Management.Automation.Runspaces;
    using Microsoft.PowerShell;
    
    namespace testConsoleRef
    {
        class Program
        {
            static void Main(string[] args)
            {
                InitialSessionState config = InitialSessionState.CreateDefault();
                config.ExecutionPolicy = ExecutionPolicy.RemoteSigned;
                config.StartupScripts.Add(@"E:\arena\dotnetApp\dotnet3\testConsoleRef\startup.ps1");
    
                int ret = ConsoleShell.Start(config, "Hello", "", new string[0]);
                Console.WriteLine(ret);
            }
        }
    }

    testConsoleRef.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" />
      </ItemGroup>
    
    </Project>

    Here is the results from execute this sample. The startup.ps1 has cd f:\tmp in it, and the working directory is changed to that after dotnet run

    [E:\arena\dotnetApp\dotnet3\testConsoleRef]
    PS:22> cat .\startup.ps1
    cd f:\tmp
    [E:\arena\dotnetApp\dotnet3\testConsoleRef]
    PS:23> dotnet run
    Hello
    
    [F:\tmp]
    PS:1> $pshome
    E:\arena\dotnetApp\dotnet3\testConsoleRef\bin\Debug\netcoreapp3.1\runtimes\win\lib\netcoreapp3.1
    [F:\tmp]
    PS:2> exit
    0
    [E:\arena\dotnetApp\dotnet3\testConsoleRef]
    PS:24> 
    
  19. iSazonov commented on Jan 11, 2020

    @iSazonov
    Collaborator

    You could add a command config.Commands.Add(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Resolution-FixedWG-DevEx-SDK

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions