Skip to content

Commit 062e15d

Browse files
dchristian3188lzybkr
authored andcommitted
Add parameter -Group to Get-Verb (PowerShell#2789)
1 parent 906c750 commit 062e15d

8 files changed

Lines changed: 160 additions & 39 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Management.Automation;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Reflection;
6+
7+
namespace Microsoft.PowerShell.Commands
8+
{
9+
10+
/// <summary>
11+
/// Implementation of the Get Verb Command
12+
/// </summary>
13+
[Cmdlet(VerbsCommon.Get, "Verb", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=160712")]
14+
[OutputType(typeof(VerbInfo))]
15+
public class GetVerbCommand : Cmdlet
16+
{
17+
/// <summary>
18+
/// Optional Verb filter
19+
/// </summary>
20+
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
21+
public string[] Verb
22+
{
23+
get; set;
24+
}
25+
26+
/// <summary>
27+
/// Optional Group filter
28+
/// </summary>
29+
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 1)]
30+
[ValidateSet("Common", "Communications", "Data", "Diagnostic", "Lifecycle", "Other", "Security")]
31+
public string[] Group
32+
{
33+
get; set;
34+
}
35+
36+
/// <summary>
37+
/// Returns a list of verbs
38+
/// </summary>
39+
protected override void ProcessRecord()
40+
{
41+
42+
Type[] verbTypes = new Type[] { typeof(VerbsCommon), typeof(VerbsCommunications), typeof(VerbsData),
43+
typeof(VerbsDiagnostic), typeof(VerbsLifecycle), typeof(VerbsOther), typeof(VerbsSecurity) };
44+
45+
Collection<WildcardPattern> matchingVerbs = SessionStateUtilities.CreateWildcardsFromStrings(
46+
this.Verb,
47+
WildcardOptions.IgnoreCase
48+
);
49+
50+
foreach (Type type in verbTypes)
51+
{
52+
string groupName = type.Name.Substring(5);
53+
if (this.Group != null)
54+
{
55+
if (!SessionStateUtilities.CollectionContainsValue(this.Group, groupName, StringComparer.OrdinalIgnoreCase))
56+
{
57+
continue;
58+
}
59+
}
60+
foreach (FieldInfo field in type.GetFields())
61+
{
62+
if (field.IsLiteral)
63+
{
64+
if (this.Verb != null)
65+
{
66+
if (!SessionStateUtilities.MatchesAnyWildcardPattern(field.Name, matchingVerbs, false))
67+
{
68+
continue;
69+
}
70+
}
71+
72+
VerbInfo verb = new VerbInfo();
73+
verb.Verb = field.Name;
74+
verb.Group = groupName;
75+
WriteObject(verb);
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}

src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
2222
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2323
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
2424
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
25-
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile"
25+
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb"
2626
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile"
2727
AliasesToExport= "fhx"
2828
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
2222
"Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2323
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
2424
"Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
25-
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime"
25+
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb"
2626
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
2727
AliasesToExport= "fhx"
2828
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

src/Modules/Windows-Full/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
2424
"Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
2525
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Show-Command", "Unblock-File",
2626
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", "Get-RunspaceDebug", "Wait-Debugger",
27-
"ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile"
27+
"ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile", "Get-Verb"
2828
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
2929
AliasesToExport= "CFS", "fhx"
3030
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

src/System.Management.Automation/engine/InitialSessionState.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,41 +4738,6 @@ .FORWARDHELPCATEGORY Cmdlet
47384738
47394739
}
47404740
4741-
";
4742-
}
4743-
4744-
4745-
internal static string GetGetVerbText()
4746-
{
4747-
return @"
4748-
param(
4749-
[Parameter(ValueFromPipeline=$true)]
4750-
[string[]]
4751-
$verb = '*'
4752-
)
4753-
begin {
4754-
$allVerbs = [System.Reflection.IntrospectionExtensions]::GetTypeInfo([PSObject]).Assembly.ExportedTypes |
4755-
Microsoft.PowerShell.Core\Where-Object {$_.Name -match '^Verbs.'} |
4756-
Microsoft.PowerShell.Utility\Get-Member -type Properties -static |
4757-
Microsoft.PowerShell.Utility\Select-Object @{
4758-
Name='Verb'
4759-
Expression = {$_.Name}
4760-
}, @{
4761-
Name='Group'
4762-
Expression = {
4763-
$str = ""$($_.TypeName)""
4764-
$str.Substring($str.LastIndexOf('Verbs') + 5)
4765-
}
4766-
}
4767-
}
4768-
process {
4769-
foreach ($v in $verb) {
4770-
$allVerbs | Microsoft.PowerShell.Core\Where-Object { $_.Verb -like $v }
4771-
}
4772-
}
4773-
# .Link
4774-
# https://go.microsoft.com/fwlink/?LinkID=160712
4775-
# .ExternalHelp System.Management.Automation.dll-help.xml
47764741
";
47774742
}
47784743

@@ -5370,7 +5335,6 @@ internal static SessionStateAliasEntry[] BuiltInAliases
53705335
#if !UNIX
53715336
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("mkdir", GetMkdirFunctionText(), isProductCode: true),
53725337
#endif
5373-
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("Get-Verb", GetGetVerbText(), isProductCode: true),
53745338
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("oss", GetOSTFunctionText(), isProductCode: true),
53755339

53765340
// Porting note: we remove the drive functions from Linux because they make no sense

src/System.Management.Automation/utils/Verbs.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,28 @@ public static class VerbsOther
581581
public const string Use = "Use";
582582
}
583583

584+
/// <summary>
585+
/// Class for Verbs and Groups
586+
/// </summary>
587+
public class VerbInfo
588+
{
589+
/// <summary>
590+
/// Verb Name
591+
/// </summary>
592+
public string Verb
593+
{
594+
get;set;
595+
}
596+
597+
/// <summary>
598+
/// Group Name
599+
/// </summary>
600+
public string Group
601+
{
602+
get;set;
603+
}
604+
}
605+
584606
internal static class Verbs
585607
{
586608
static Verbs()

src/vs-csproj/Microsoft.PowerShell.Commands.Utility.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@
580580
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\singleshell\installer\MshUtilityMshSnapin.cs">
581581
<Link>singleshell\installer\MshUtilityMshSnapin.cs</Link>
582582
</Compile>
583+
<Compile Include="commands\utility\GetVerbCommand.cs" />
583584
</ItemGroup>
584585
<ItemGroup>
585586
<None Include="..\Microsoft.PowerShell.Commands.Utility\map.json">
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Describe "Get-Verb" -Tags "CI" {
2+
3+
It "Should get a list of Verbs" {
4+
Get-Verb | Should not be $null
5+
}
6+
7+
It "Should get a specific verb" {
8+
@(Get-Verb -Verb Add).Count | Should be 1
9+
@(Get-Verb -Verb Add -Group Common).Count | Should be 1
10+
}
11+
12+
It "Should get a specific group" {
13+
(Get-Verb -Group Common).Group | Sort-Object -Unique | Should be Common
14+
}
15+
16+
It "Should not return duplicate Verbs with Verb paramater" {
17+
$dups = Get-Verb -Verb Add,ad*,a*
18+
$unique = $dups |
19+
Select-Object -Property * -Unique
20+
$dups.Count | Should be $unique.Count
21+
}
22+
23+
It "Should not return duplicate Verbs with Group paramater" {
24+
$dupVerbs = Get-Verb -Group Data,Data
25+
$uniqueVerbs = $dupVerbs |
26+
Select-Object -Property * -Unique
27+
$dupVerbs.Count | Should be $uniqueVerbs.Count
28+
}
29+
30+
It "Should filter using the Verb parameter" {
31+
Get-Verb -Verb fakeVerbNeverExists | Should BeNullOrEmpty
32+
}
33+
34+
It "Should not accept Groups that are not in the validate set" {
35+
try{
36+
Get-Verb -Group FakeGroupNeverExists -ErrorAction Stop
37+
throw "Expected error did not occur"
38+
}
39+
Catch{
40+
$PSItem.FullyQualifiedErrorId | Should be 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetVerbCommand'
41+
}
42+
}
43+
44+
It "Accept all valid verb groups" {
45+
$groups = ([System.Reflection.IntrospectionExtensions]::GetTypeInfo([PSObject]).Assembly.ExportedTypes |
46+
Where-Object {$_.Name -match '^Verbs.'} |
47+
Select-Object -Property @{Name='VerbGroup';Expression={$_.Name.Substring(5)}}).VerbGroup
48+
ForEach($group in $groups)
49+
{
50+
{Get-Verb -Group $group} | Should Not Throw
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)