[release/v7.6.7] Add GetImportModuleCommand method and session restriction check in ShowCommand (#28088)

Co-authored-by: Travis Plunk <travis.plunk@microsoft.com>
This commit is contained in:
Aditya PatwardhanandTravis Plunk authored and GitHub committed 2026-09-24 10:21:36 -07:00
1 parent 8cc67bbf87
commit 6cb2755da0
3 files changed
+41

No files matched your search

@@ -489,6 +489,37 @@ Function PSGetSerializedShowCommandInfo
@"Remove-Item -Path 'function:\PSGetSerializedShowCommandInfo' -Force");
}
/// <summary>
/// Gets the command to be run to in order to import a module and refresh the command data.
/// </summary>
/// <param name="module">Module we want to import.</param>
/// <param name="isRemoteRunspace">Boolean flag determining whether Show-Command is queried in the local or remote runspace scenario.</param>
/// <param name="isFirstChance">Boolean flag to indicate that it is the second attempt to query Show-Command data.</param>
/// <returns>The command to be run to in order to import a module and refresh the command data.</returns>
internal static string GetImportModuleCommand(string module, bool isRemoteRunspace = false, bool isFirstChance = true)
{
string scriptBase = "Import-Module " + ShowCommandHelper.SingleQuote(module);
if (isRemoteRunspace)
{
if (isFirstChance)
{
scriptBase += ";@(Get-Command " + ShowCommandHelper.CommandTypeSegment + @" -ShowCommandInfo )";
}
else
{
scriptBase += GetSerializedCommandScript();
}
}
else
{
scriptBase += ";@(Get-Command " + ShowCommandHelper.CommandTypeSegment + ")";
}
scriptBase += ShowCommandHelper.GetGetModuleSuffix();
return scriptBase;
}
/// <summary>
/// Gets the command to be run in order to show help for a command.
/// </summary>
@@ -170,6 +170,13 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
protected override void BeginProcessing()
{
if (Utils.IsSessionRestricted(this.Context))
{
Exception e = new InvalidOperationException(HelpErrors.NotAllowedInRestrictedRemoting);
ErrorRecord errorRecord = new ErrorRecord(e, "CommandNotAllowed", ErrorCategory.InvalidOperation, null);
this.ThrowTerminatingError(errorRecord);
}
_showCommandProxy = new ShowCommandProxy(this);
if (_showCommandProxy.ScreenHeight < this.Height)
@@ -190,4 +190,7 @@ To update these Help topics, start PowerShell by using the "Run as Administrator
<data name="NoNetworkCommands" xml:space="preserve">
<value>Cannot get help from a network location when in a restricted session.</value>
</data>
<data name="NotAllowedInRestrictedRemoting" xml:space="preserve">
<value>Command not allowed in a restricted session.</value>
</data>
</root>