Add parameter -Group to Get-Verb (#2789)

This commit is contained in:
David Christian 2017-01-03 15:13:42 -08:00 committed by Jason Shirk
parent 906c750386
commit 062e15def2
8 changed files with 160 additions and 39 deletions

View file

@ -0,0 +1,81 @@
using System;
using System.Management.Automation;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Implementation of the Get Verb Command
/// </summary>
[Cmdlet(VerbsCommon.Get, "Verb", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=160712")]
[OutputType(typeof(VerbInfo))]
public class GetVerbCommand : Cmdlet
{
/// <summary>
/// Optional Verb filter
/// </summary>
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public string[] Verb
{
get; set;
}
/// <summary>
/// Optional Group filter
/// </summary>
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 1)]
[ValidateSet("Common", "Communications", "Data", "Diagnostic", "Lifecycle", "Other", "Security")]
public string[] Group
{
get; set;
}
/// <summary>
/// Returns a list of verbs
/// </summary>
protected override void ProcessRecord()
{
Type[] verbTypes = new Type[] { typeof(VerbsCommon), typeof(VerbsCommunications), typeof(VerbsData),
typeof(VerbsDiagnostic), typeof(VerbsLifecycle), typeof(VerbsOther), typeof(VerbsSecurity) };
Collection<WildcardPattern> matchingVerbs = SessionStateUtilities.CreateWildcardsFromStrings(
this.Verb,
WildcardOptions.IgnoreCase
);
foreach (Type type in verbTypes)
{
string groupName = type.Name.Substring(5);
if (this.Group != null)
{
if (!SessionStateUtilities.CollectionContainsValue(this.Group, groupName, StringComparer.OrdinalIgnoreCase))
{
continue;
}
}
foreach (FieldInfo field in type.GetFields())
{
if (field.IsLiteral)
{
if (this.Verb != null)
{
if (!SessionStateUtilities.MatchesAnyWildcardPattern(field.Name, matchingVerbs, false))
{
continue;
}
}
VerbInfo verb = new VerbInfo();
verb.Verb = field.Name;
verb.Group = groupName;
WriteObject(verb);
}
}
}
}
}
}

View file

@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile"
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb"
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile"
AliasesToExport= "fhx"
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

View file

@ -22,7 +22,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Get-TraceSource", "Set-TraceSource", "Trace-Command",
"Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime"
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb"
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
AliasesToExport= "fhx"
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

View file

@ -24,7 +24,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Show-Command", "Unblock-File",
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", "Get-RunspaceDebug", "Wait-Debugger",
"ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile"
"ConvertFrom-String", "Convert-String" , "Get-Uptime", "New-TemporaryFile", "Get-Verb"
FunctionsToExport= "Get-FileHash", "Format-Hex", "Import-PowerShellDataFile", "ConvertFrom-SddlString"
AliasesToExport= "CFS", "fhx"
NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1"

View file

@ -4738,41 +4738,6 @@ end {
}
";
}
internal static string GetGetVerbText()
{
return @"
param(
[Parameter(ValueFromPipeline=$true)]
[string[]]
$verb = '*'
)
begin {
$allVerbs = [System.Reflection.IntrospectionExtensions]::GetTypeInfo([PSObject]).Assembly.ExportedTypes |
Microsoft.PowerShell.Core\Where-Object {$_.Name -match '^Verbs.'} |
Microsoft.PowerShell.Utility\Get-Member -type Properties -static |
Microsoft.PowerShell.Utility\Select-Object @{
Name='Verb'
Expression = {$_.Name}
}, @{
Name='Group'
Expression = {
$str = ""$($_.TypeName)""
$str.Substring($str.LastIndexOf('Verbs') + 5)
}
}
}
process {
foreach ($v in $verb) {
$allVerbs | Microsoft.PowerShell.Core\Where-Object { $_.Verb -like $v }
}
}
# .Link
# https://go.microsoft.com/fwlink/?LinkID=160712
# .ExternalHelp System.Management.Automation.dll-help.xml
";
}
@ -5370,7 +5335,6 @@ if($paths) {
#if !UNIX
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("mkdir", GetMkdirFunctionText(), isProductCode: true),
#endif
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("Get-Verb", GetGetVerbText(), isProductCode: true),
SessionStateFunctionEntry.GetDelayParsedFunctionEntry("oss", GetOSTFunctionText(), isProductCode: true),
// Porting note: we remove the drive functions from Linux because they make no sense

View file

@ -581,6 +581,28 @@ namespace System.Management.Automation
public const string Use = "Use";
}
/// <summary>
/// Class for Verbs and Groups
/// </summary>
public class VerbInfo
{
/// <summary>
/// Verb Name
/// </summary>
public string Verb
{
get;set;
}
/// <summary>
/// Group Name
/// </summary>
public string Group
{
get;set;
}
}
internal static class Verbs
{
static Verbs()

View file

@ -580,6 +580,7 @@
<Compile Include="..\Microsoft.PowerShell.Commands.Utility\singleshell\installer\MshUtilityMshSnapin.cs">
<Link>singleshell\installer\MshUtilityMshSnapin.cs</Link>
</Compile>
<Compile Include="commands\utility\GetVerbCommand.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\Microsoft.PowerShell.Commands.Utility\map.json">

View file

@ -0,0 +1,53 @@
Describe "Get-Verb" -Tags "CI" {
It "Should get a list of Verbs" {
Get-Verb | Should not be $null
}
It "Should get a specific verb" {
@(Get-Verb -Verb Add).Count | Should be 1
@(Get-Verb -Verb Add -Group Common).Count | Should be 1
}
It "Should get a specific group" {
(Get-Verb -Group Common).Group | Sort-Object -Unique | Should be Common
}
It "Should not return duplicate Verbs with Verb paramater" {
$dups = Get-Verb -Verb Add,ad*,a*
$unique = $dups |
Select-Object -Property * -Unique
$dups.Count | Should be $unique.Count
}
It "Should not return duplicate Verbs with Group paramater" {
$dupVerbs = Get-Verb -Group Data,Data
$uniqueVerbs = $dupVerbs |
Select-Object -Property * -Unique
$dupVerbs.Count | Should be $uniqueVerbs.Count
}
It "Should filter using the Verb parameter" {
Get-Verb -Verb fakeVerbNeverExists | Should BeNullOrEmpty
}
It "Should not accept Groups that are not in the validate set" {
try{
Get-Verb -Group FakeGroupNeverExists -ErrorAction Stop
throw "Expected error did not occur"
}
Catch{
$PSItem.FullyQualifiedErrorId | Should be 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetVerbCommand'
}
}
It "Accept all valid verb groups" {
$groups = ([System.Reflection.IntrospectionExtensions]::GetTypeInfo([PSObject]).Assembly.ExportedTypes |
Where-Object {$_.Name -match '^Verbs.'} |
Select-Object -Property @{Name='VerbGroup';Expression={$_.Name.Substring(5)}}).VerbGroup
ForEach($group in $groups)
{
{Get-Verb -Group $group} | Should Not Throw
}
}
}