You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request - Bring back ConsoleShell functionallity #9734
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!
It seems that when we removed RunspaceConfiguration, we should have added back an overload to accept InitialSessionState. cc James Truher (@JamesWTruher)
Reacted by Rain Sallow (/u/ta11ow), asrosent, Patrick Meinecke and rubyFeedback
asrosentSteve 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
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. 🙂
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...
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.
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] { });
}
}
}
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:
// ThrowOnRunspaceOpenError handling is done by ProcessPowerShellCommand
returnexceptionToReturn;
}
}
}
returnnull;
}
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:
Activity
iSazonov commentedon May 27, 2019
Steve Lee (@SteveL-MSFT) Could you please comment the scenario and feature request? What is recommended way to create custom PowerShell console shell?
SteveL-MSFT commentedon May 29, 2019
It seems that when we removed RunspaceConfiguration, we should have added back an overload to accept InitialSessionState. cc James Truher (@JamesWTruher)
ritchxu commentedon Oct 22, 2019
asrosent Steve Lee (@SteveL-MSFT) is
ConsoleShellclass included inMicrosoft.PowerShell.SDKnuget package? I seem to have hard time finding it with version 6.2.3vexx32 commentedon Oct 22, 2019
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 when7.0.0gets 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. 🙂
ritchxu commentedon Oct 22, 2019
Thanks Rain Sallow (/u/ta11ow) (@vexx32) for pointing that out, the related PR adds back taking
InitialSessionStateas parameter, but what I'm experiencing is I couldn't even find theConsoleShellclass. But before the PR, the class still exists publicly, so I was thinking if I'm looking at the wrong package...vexx32 commentedon Oct 22, 2019
Oh, that's a good point, my apologies! Not sure... have to defer to Steve Lee (@SteveL-MSFT) on that point. 🙂
SteveL-MSFT commentedon Oct 22, 2019
The
Microsoft.PowerShell.ConsoleShellclass has always been there. The only change was to add back an overload:ritchxu commentedon Oct 22, 2019
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.Sdkpackage?https://www.nuget.org/packages/Microsoft.PowerShell.SDK/
vexx32 commentedon Oct 22, 2019
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.
11 remaining items
SteveL-MSFT commentedon Oct 25, 2019
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.
ritchxu commentedon Nov 6, 2019
Steve Lee (@SteveL-MSFT) with preview-5, I updated my sample code to
It seems like
StartupScriptsare not executed?daxian-dbw commentedon Jan 9, 2020
Ray Xu (@ritchxu)
StartupScriptsare executed, but the results are not collected and not displayed. The code that executes it is here:PowerShell/src/System.Management.Automation/engine/InitialSessionState.cs
Lines 2893 to 2903 in 5ad4d2c
iSazonov commentedon Jan 10, 2020
Dongbo Wang (@daxian-dbw) Maybe add logging in the code or script block logging already works here?
ritchxu commentedon Jan 10, 2020
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:
Test.csproj
daxian-dbw commentedon Jan 10, 2020
Ray Xu (@ritchxu)
initialSessionState.StartupScriptsis for you to run one more more script files (.ps1), not script code directly. Here is the code to process the startup scripts:PowerShell/src/System.Management.Automation/engine/InitialSessionState.cs
Lines 2858 to 2876 in 5ad4d2c
This line
psToInvoke.AddCommand(new Command(startupScript, false, false));creates a new command by specifying that it's not a script (the firstfalseargument).The name of that property is unfortunately vague 😦 The following is an example to use this property:
Program.cs:
testConsoleRef.csproj:
Here is the results from execute this sample. The
startup.ps1hascd f:\tmpin it, and the working directory is changed to that afterdotnet runiSazonov commentedon Jan 11, 2020
You could add a command
config.Commands.Add(...)