Geliştirilmiş Bir Get-Fiil Komutu
Özellikle işlevler geliştirirken PowerShell komut dosyası oluşturma için önerilen en iyi uygulama, standart Fiil-İsim adlandırma kuralına uymaktır. Fiil, onaylanmış .NET fiilleri listesinden bir değer olmalıdır. Bu listeyi görmenin kolay yolu Get-Verb cmdlet'idir. Sonuç aynı zamanda Güvenlik veya Yaşam Döngüsü gibi fiil grubunu veya kategorisini de gösterecektir. Belirli bir grup için filtreleme yapmak istiyorsanız Where-Object ifadesini kullanmanız gerekiyordu. Gerçekten o kadar da zor değil ama Get-Verb'in bir veya daha fazla grubu belirtmenize izin verecek kendi versiyonunu yapmaya karar verdim.
#requires -version 4.0
<#
This is a copy of:
CommandType Name ModuleName
----------- ---- ----------
Function Get-Verb
Created: 8/10/2015
Author : Jeff Hicks @JeffHicks
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
#>
Function Get-MyVerb {
<#
.SYNOPSIS
Gets approved Windows PowerShell verbs.
.DESCRIPTION
The Get-MyVerb function gets verbs that are approved for use in Windows PowerShell commands.
Windows PowerShell recommends that cmdlet and function names have the Verb-Noun format and include an approved verb. This practice makes command names more consistent and predictable, and easier to use, especially for users who do not speak English as a first language.
Commands that use unapproved verbs run in Windows PowerShell. However, when you import a module that includes a command with an unapproved verb in its name, the Import-Module command displays a warning message.
This command is a variation on Get-Verb that allows you to limit verbs to a category.
NOTE: The verb list that Get-MyVerb returns might not be complete. For an updated list of approved Windows PowerShell verbs with descriptions, see "Cmdlet Verbs" in MSDN at http://go.microsoft.com/fwlink/?LinkID=160773.
.PARAMETER GROUP
Each Windows PowerShell verb is assigned to one of the following groups.
-- Common: Define generic actions that can apply to almost any cmdlet, such as Add.
-- Communications: Define actions that apply to communications, such as Connect.
-- Data: Define actions that apply to data handling, such as Backup.
-- Diagnostic: Define actions that apply to diagnostics, such as Debug.
-- Lifecycle: Define actions that apply to the lifecycle of a cmdlet, such as Complete.
-- Security: Define actions that apply to security, such as Revoke.
-- Other: Define other types of actions.
The default is everything.
.EXAMPLE
PS C:\> Get-MyVerb
Description
-----------
This command gets all approved verbs.
.EXAMPLE
PS C:\> Get-MyVerb un*
Verb Group
---- -----
Undo Common
Unlock Common
Unpublish Data
Uninstall Lifecycle
Unregister Lifecycle
Unblock Security
Unprotect Security
Description
-----------
This command gets all approved verbs that begin with "un".
.EXAMPLE
PS C:\> Get-MyVerb -Group "Security"
Verb Group
---- -----
Block Security
Grant Security
Protect Security
Revoke Security
Unblock Security
Unprotect Security
This command gets all approved verbs in the Security group.
.EXAMPLE
PS C:\> get-command -module MyModule | where { (Get-MyVerb $_.Verb) -eq $null }
This command finds all commands in a module that have unapproved verbs.
.EXAMPLE
PS C:\> $approvedVerbs = Get-MyVerb | foreach {$_.verb}
PS C:\> $myVerbs = get-command -module MyModule | foreach {$_.verb}
PS C:\> ($myVerbs | foreach {$approvedVerbs -contains $_}) -contains $false
True
Does MyModule export functions with unapproved verbs?
PS C:\> ($myverbs | where {$approvedVerbs -notcontains $_})
ForEach
Sort
Tee
Where
These commands detect unapproved verbs in a module and tell which unapproved verbs were detected in the module.
.NOTES
Get-MyVerb returns a modified version of a Microsoft.PowerShell.Commands.MemberDefinition object. The object does not have the standard properties of a MemberDefinition object. Instead it has Verb and Group properties. The Verb property contains a string with the verb name. The Group property contains a string with the verb group.
Windows PowerShell verbs are assigned to a group based on their most common use. The groups are designed to make the verbs easy to find and compare, not to restrict their use. You can use any approved verb for any type of command.
Some of the cmdlets that are installed with Windows PowerShell, such as Tee-Object and Where-Object, use unapproved verbs. These cmdlets are considered to be historic exceptions and their verbs are classified as "reserved."
Learn more about PowerShell:
Essential PowerShell Learning Resources
.INPUTS
String
.OUTPUTS
Selected.Microsoft.PowerShell.Commands.MemberDefinition
.LINK
Get-Verb
Import-Module
.LINK
https://msdn.microsoft.com/en-us/library/ms714428%28VS.85%29.aspx
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[string[]]$Verb,
[ValidateSet("Common","Communications","Data","Diagnostic","LifeCycle","Security","Other")]
[string[]]$Group
)
Begin {
Write-Verbose "Starting $($MyInvocation.Mycommand)"
Write-Verbose "Using parameter set $($PSCmdlet.ParameterSetName)"
} #begin
Process {
#remove Group from PSBoundParameters because Get-Verb won't recognize it.
if ($Group) {
$PSBoundParameters.Remove("Group") | Out-Null
Write-Verbose "Filtering by group: $($group -join '|')"
}
Write-Verbose "PSBoundParameters: $($PSBoundParameters | out-string)"
Get-Verb @PSBoundParameters | where {$_.group -match ($Group -join "|")}
} #process
End {
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #end function Get-MyVerbİşlev aslında Get-Verb için bir sarmalayıcıdır. Aynı parametreleri ve benim eklememi kullanıyor. Yardım aynı zamanda Get-Verb'den geliyor, ancak kendi komut versiyonumu yansıtacak şekilde biraz değiştirdim. Get-MyVerb'ü Get-Verb'de olduğu gibi kullanabilirsiniz veya bir veya daha fazla grup belirtebilirsiniz.
Son gelişme, Get-MyVerb için yardımı -Online ile çalıştırırsanız, .NET fiilleri hakkında ayrıntılı bilgi içeren MSDN belgelerini alacaksınız. Faydalı bulacağınızı düşünüyorum.
Keyfini çıkarın ve her zaman olduğu gibi yorum veya sorularınızı bekliyoruz.
