From 6cb2755da06ffc9346a7888cc526ae4854f5b50a Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 24 Sep 2026 10:21:36 -0700 Subject: [PATCH] [release/v7.6.7] Add GetImportModuleCommand method and session restriction check in ShowCommand (#28088) Co-authored-by: Travis Plunk --- .../commandHelpers/ShowCommandHelper.cs | 31 +++++++++++++++++++ .../utility/ShowCommand/ShowCommand.cs | 7 +++++ .../resources/HelpErrors.resx | 3 ++ 3 files changed, 41 insertions(+) diff --git a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs index 32c68e961..13dfe839b 100644 --- a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs +++ b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs @@ -489,6 +489,37 @@ Function PSGetSerializedShowCommandInfo @"Remove-Item -Path 'function:\PSGetSerializedShowCommandInfo' -Force"); } + /// + /// Gets the command to be run to in order to import a module and refresh the command data. + /// + /// Module we want to import. + /// Boolean flag determining whether Show-Command is queried in the local or remote runspace scenario. + /// Boolean flag to indicate that it is the second attempt to query Show-Command data. + /// The command to be run to in order to import a module and refresh the command data. + 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; + } + /// /// Gets the command to be run in order to show help for a command. /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs index 60b05807d..01267bab6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs @@ -170,6 +170,13 @@ namespace Microsoft.PowerShell.Commands /// 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) diff --git a/src/System.Management.Automation/resources/HelpErrors.resx b/src/System.Management.Automation/resources/HelpErrors.resx index cf8f8fa88..029548fb9 100644 --- a/src/System.Management.Automation/resources/HelpErrors.resx +++ b/src/System.Management.Automation/resources/HelpErrors.resx @@ -190,4 +190,7 @@ To update these Help topics, start PowerShell by using the "Run as Administrator Cannot get help from a network location when in a restricted session. + + Command not allowed in a restricted session. +