Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExcludeModule parameter to Get-Command #18955

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,12 @@ internal static string ConcatenateStringPathArguments(CommandElementAst stringAs
break;
}

if (parameterName.Equals("ExcludeModule", StringComparison.OrdinalIgnoreCase))
{
NativeCompletionGetCommand(context, moduleName: null, parameterName, result);
break;
}

if (parameterName.Equals("Name", StringComparison.OrdinalIgnoreCase))
{
var moduleNames = NativeCommandArgumentCompletion_ExtractSecondaryArgument(boundArguments, "Module");
Expand Down Expand Up @@ -3046,7 +3052,9 @@ private static void NativeCompletionGetCommand(CompletionContext context, string

result.Add(CompletionResult.Null);
}
else if (!string.IsNullOrEmpty(paramName) && paramName.Equals("Module", StringComparison.OrdinalIgnoreCase))
else if (!string.IsNullOrEmpty(paramName)
&& (paramName.Equals("Module", StringComparison.OrdinalIgnoreCase)
|| paramName.Equals("ExcludeModule", StringComparison.OrdinalIgnoreCase)))
{
CompleteModule(context, result);
}
Expand Down
38 changes: 38 additions & 0 deletions src/System.Management.Automation/engine/GetCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ public string[] Module
private string[] _modules = Array.Empty<string>();
private bool _isModuleSpecified = false;

/// <summary>
/// Gets or sets the ExcludeModule parameter to the cmdlet.
/// </summary>
[Parameter()]
public string[] ExcludeModule
{
get
{
return _excludedModules;
}

set
{
value ??= Array.Empty<string>();

_excludedModules = value;
_excludedModulePatterns = null;
}
}

private string[] _excludedModules = Array.Empty<string>();

/// <summary>
/// Gets or sets the FullyQualifiedModule parameter to the cmdlet.
/// </summary>
Expand Down Expand Up @@ -404,6 +426,7 @@ protected override void ProcessRecord()

// Initialize the module patterns
_modulePatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(Module, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
_excludedModulePatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(ExcludeModule, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);

switch (ParameterSetName)
{
Expand Down Expand Up @@ -702,6 +725,13 @@ private bool IsNounVerbMatch(CommandInfo command)

if (!string.IsNullOrEmpty(command.ModuleName))
{
if (_excludedModulePatterns is not null
&& _excludedModulePatterns.Count > 0
&& SessionStateUtilities.MatchesAnyWildcardPattern(command.ModuleName, _excludedModulePatterns, true))
{
break;
}

if (_isFullyQualifiedModuleSpecified)
{
if (!_moduleSpecifications.Any(
Expand Down Expand Up @@ -1271,6 +1301,13 @@ private bool IsCommandMatch(ref CommandInfo current, out bool isDuplicate)
}
else
{
if (_excludedModulePatterns is not null
&& _excludedModulePatterns.Count > 0
&& SessionStateUtilities.MatchesAnyWildcardPattern(current.ModuleName, _excludedModulePatterns, true))
{
return false;
}

if (_isFullyQualifiedModuleSpecified)
{
bool foundModuleMatch = false;
Expand Down Expand Up @@ -1530,6 +1567,7 @@ private bool IsCommandInResult(CommandInfo command)
private Collection<WildcardPattern> _verbPatterns;
private Collection<WildcardPattern> _nounPatterns;
private Collection<WildcardPattern> _modulePatterns;
private Collection<WildcardPattern> _excludedModulePatterns;

#if LEGACYTELEMETRY
private Stopwatch _timer = new Stopwatch();
Expand Down
1 change: 1 addition & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ class InheritedClassTest : System.Attribute
@{ inputStr = 'gmo Microsoft.PowerShell.U'; expected = 'Microsoft.PowerShell.Utility'; setup = $null }
@{ inputStr = 'rmo Microsoft.PowerShell.U'; expected = 'Microsoft.PowerShell.Utility'; setup = $null }
@{ inputStr = 'gcm -Module Microsoft.PowerShell.U'; expected = 'Microsoft.PowerShell.Utility'; setup = $null }
@{ inputStr = 'gcm -ExcludeModule Microsoft.PowerShell.U'; expected = 'Microsoft.PowerShell.Utility'; setup = $null }
@{ inputStr = 'gmo -list PackageM'; expected = 'PackageManagement'; setup = $null }
@{ inputStr = 'gcm -Module PackageManagement Find-Pac'; expected = 'Find-Package'; setup = $null }
@{ inputStr = 'ipmo PackageM'; expected = 'PackageManagement'; setup = $null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,9 @@ Describe "Get-Command Tests" -Tags "CI" {
$result.Count | Should -Be 2
$result.Name | Should -Be "Add-Content","Get-Content"
}

It "Excluding modules works" {
MartinGC94 marked this conversation as resolved.
Show resolved Hide resolved
$result = Get-Command -Name Get-Command -ExcludeModule Microsoft.PowerShell.Core
$result | Should -Be $null
}
}
Loading