[玩转系统] 改进的 Get-Verb 命令
作者:精品下载站 日期:2024-12-14 07:46:35 浏览:14 分类:玩电脑
改进的 Get-Verb 命令
PowerShell 脚本的推荐最佳实践(尤其是在开发函数时)是遵循标准动词-名词命名约定。动词应该是已批准的 .NET 动词列表中的值。查看该列表的简单方法是使用 Get-Verb cmdlet。结果还将指示动词组或类别,例如安全性或生命周期。如果您想过滤特定组,则需要使用Where-Object 表达式。这确实不是那么困难,但我决定制作一个我自己的 Get-Verb 版本,它可以让您指定一个或多个组。
#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
该函数本质上是 Get-Verb 的包装器。它使用相同的参数加上我的添加。该帮助也来自 Get-Verb,尽管我稍微修改了它以反映我的命令版本。您可以像使用 Get-Verb 一样使用 Get-MyVerb,也可以指定一个或多个组。
最后一项改进是,如果您使用 -Online 运行 Get-MyVerb 的帮助,您将获得有关 .NET 动词的详细信息的 MSDN 文档。我想你会发现它很有帮助。
享受并一如既往地欢迎评论或提问。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag