Add GetImportModuleCommand method and session restriction check in ShowCommand - #25557
Merged
Dongbo Wang (daxian-dbw) merged 3 commits intoSep 2, 2026
Merged
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Dongbo Wang (daxian-dbw)
marked this pull request as ready for review
September 1, 2026 20:00
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Dongbo Wang (daxian-dbw)
approved these changes
Sep 1, 2026
Copilot started reviewing on behalf of
Dongbo Wang (daxian-dbw)
September 1, 2026 20:01
View session
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR addresses #25556 by restoring a missing helper used by Show-Command to import modules and refresh command metadata (used by the UI when showing command details), and by preventing Show-Command from running in restricted sessions where it isn’t supported.
Changes:
- Reintroduces
GetImportModuleCommandscript generation logic inShowCommandHelperto support module import + command refresh (including remote-runspace scenarios). - Adds a restricted-session guard in
ShowCommandBeginProcessingto terminate early with a clear error. - Adds a new localized resource string for the restricted-session error message.
File summaries
| File | Description |
|---|---|
| src/System.Management.Automation/resources/HelpErrors.resx | Adds a new error string used when blocking execution in restricted sessions. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs | Blocks Show-Command in restricted sessions via Utils.IsSessionRestricted(...) and throws a terminating error. |
| src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs | Restores GetImportModuleCommand to generate import/refresh scripts for Show-Command module loading flows. |
Review details
Suppressed comments (2)
src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs:511
- In the remote runspace fallback path, the serialized command script is concatenated directly after
Import-Module ...without a statement separator, producing an invalid PowerShell script (e.g.,Import-Module 'X'@( ... )). Add a;before the serialized script so it runs as a separate statement.
scriptBase += GetSerializedCommandScript();
src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs:498
isFirstChanceis documented as indicating the second attempt, but the name (and default value) indicate the first attempt. This mismatch can easily lead to incorrect future call sites or changes.
/// <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>
- Files reviewed: 3/3 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Ilya (iSazonov)
approved these changes
Sep 2, 2026
Dongbo Wang (daxian-dbw)
merged commit Sep 2, 2026
eef0852
into
PowerShell:master
38 of 39 checks passed
3 of 9 tasks
3 of 9 tasks
3 of 9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Fix #25556
This pull request introduces two main changes to improve functionality and error handling in the
ShowCommandfeature. The first change brings back a helper method to generate module import scripts with support for remote and local runspaces.The second change enhances error handling by restricting the
ShowCommandfeature in specific remoting scenarios.New functionality:
src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs: Added theGetImportModuleCommandmethod to generate a script for importing a module and refreshing command data. This method supports both local and remote runspaces and handles first and subsequent attempts differently.Improved error handling:
src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs: Added a check in theBeginProcessingmethod to prevent theShowCommandfeature from running in restricted remoting sessions. If the session is restricted, a terminating error is thrown with an appropriate error message.PR Context
Fixes #25556
Introduced in #20593
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header