mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add ExcludeModule parameter to Get-Command (#18955)
This commit is contained in:
@@ -2173,6 +2173,12 @@ namespace System.Management.Automation
|
||||
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");
|
||||
@@ -3072,7 +3078,9 @@ namespace System.Management.Automation
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -140,6 +140,28 @@ namespace Microsoft.PowerShell.Commands
|
||||
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>
|
||||
@@ -404,6 +426,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
// Initialize the module patterns
|
||||
_modulePatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(Module, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
|
||||
_excludedModulePatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(ExcludeModule, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
|
||||
|
||||
switch (ParameterSetName)
|
||||
{
|
||||
@@ -702,6 +725,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
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(
|
||||
@@ -1271,6 +1301,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_excludedModulePatterns is not null
|
||||
&& _excludedModulePatterns.Count > 0
|
||||
&& SessionStateUtilities.MatchesAnyWildcardPattern(current.ModuleName, _excludedModulePatterns, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_isFullyQualifiedModuleSpecified)
|
||||
{
|
||||
bool foundModuleMatch = false;
|
||||
@@ -1530,6 +1567,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
private Collection<WildcardPattern> _verbPatterns;
|
||||
private Collection<WildcardPattern> _nounPatterns;
|
||||
private Collection<WildcardPattern> _modulePatterns;
|
||||
private Collection<WildcardPattern> _excludedModulePatterns;
|
||||
|
||||
#if LEGACYTELEMETRY
|
||||
private Stopwatch _timer = new Stopwatch();
|
||||
|
||||
@@ -1674,6 +1674,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 }
|
||||
|
||||
@@ -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" {
|
||||
$result = Get-Command -Name Get-Command -ExcludeModule Microsoft.PowerShell.Core
|
||||
$result | Should -Be $null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user