PowerShell中对函数参数的命名建议

所属分类: 脚本专栏 / PowerShell 阅读数: 1009
收藏 0 赞 0 分享

PowerShell自定义函数的参数没有具体的命名规则,那么怎么做才能让参数的命名更加科学可持续。

我们应该怎么来命名函数的参数名称呢?有没有一个可供参考的列表?当然微软没有给出来。但是我们可以来理一个列表出来。微软自带了大量的cmdlet,这些cmdlet使用的参数有什么规律呢?我们用下面一段命令来统计一下。

复制代码 代码如下:

Get-Command -CommandType Cmdlet |
ForEach-Object { $_.Parameters } |
ForEach-Object { $_.Keys } |
Group-Object -NoElement |
Sort-Object Count, Name -Descending |
Select-Object -Skip 11 |
Where-Object { $_.Count -gt 1 }

这段代码的作用就是把所有的cmdlet使用的参数名称来做一个统计,看看哪些名称的参数用得比较多,然后按从多到少的顺序做一个列表。小编这边运行后的结果如下:
复制代码 代码如下:

Count Name
----- ----
   69 Force
   56 InputObject
   53 Credential
   52 Path
   48 PassThru
   43 LiteralPath
   35 UseTransaction
   33 Exclude
   32 Include
   30 ComputerName
   28 Filter
   26 Id
   14 Scope
   14 Property
   14 InstanceId
   13 Value
   13 Authentication
   12 ThrottleLimit
   12 Description
   12 ArgumentList
   10 Session
   10 Encoding
    9 SourceIdentifier
    8 Wait
    8 Namespace
    8 DisplayName
    8 CertificateThumbprint
    8 AsJob
    7 UseSSL
    7 Stream
    7 State
    7 Port
    7 NoClobber
    7 Impersonation
    7 FilePath
    6 TypeName
    6 Timeout
    6 SessionOption
    6 Option
    6 Module
    6 Message
    6 LogName
    6 Job
    6 ConnectionUri
    6 ConfigurationName
    6 Command
    6 Class
    6 ApplicationName
    6 AllowRedirection
    5 View
    5 Variable
    5 Source
    5 Recurse
    5 PSProvider
    5 Delimiter
    4 UseDefaultCredentials
    4 UseCulture
    4 UICulture
    4 StackName
    4 ShowError
    4 NewName
    4 MessageData
    4 Locale
    4 GroupBy
    4 Expand
    4 EnableAllPrivileges
    4 DisplayError
    4 Destination
    4 Depth
    4 Culture
    4 Count
    4 Certificate
    4 Category
    4 CaseSensitive
    4 Body
    4 Authority
    4 Append
    4 Action
    3 Width
    3 Uri
    3 SupportEvent
    3 Server
    3 SecurityDescriptorSddl
    3 ScriptBlock
    3 Restart
    3 PSVersion
    3 ProxyCredential
    3 OutputBufferingMode
    3 NoTypeInformation
    3 NoServiceRestart
    3 Newest
    3 ModulesToImport
    3 MemberType
    3 MaxTriggerCount
    3 MaximumRedirection
    3 LocalCredential
    3 List
    3 Function
    3 Forward
    3 EnableNetworkAccess
    3 CommandType
    3 Cmdlet
    3 Breakpoint
    3 AsString
    3 AssemblyName
    3 As
    2 WebSession
    2 Visibility
    2 Verb
    2 UseSharedProcess
    2 UserAgent
    2 UnjoinDomainCredential
    2 Unique
    2 TypesToProcess
    2 TypeData
    2 Type
    2 TransportOption
    2 TransferEncoding
    2 TotalCount
    2 Title
    2 TimeoutSec
    2 ThreadOptions
    2 ThreadApartmentState
    2 SubscriptionId
    2 Strict
    2 Status
    2 StartupType
    2 StartupScript
    2 SkipNetworkProfileCheck
    2 Skip
    2 ShowSecurityDescriptorUI
    2 SessionVariable
    2 SessionTypeOption
    2 SessionType
    2 SecondValue
    2 Seconds
    2 ScriptsToProcess
    2 Script
    2 RunAsCredential
    2 RestorePoint
    2 Resolve
    2 Quiet
    2 Query
    2 PSSession
    2 PSHost
    2 ProxyUseDefaultCredent...
    2 Proxy
    2 ProcessorArchitecture
    2 PrependPath
    2 Prefix
    2 PowerShellVersion
    2 OutFile
    2 Off
    2 NotMatch
    2 ModuleInfo
    2 Minimum
    2 Method
    2 MemberName
    2 MaximumReceivedObjectS...
    2 MaximumReceivedDataSiz...
    2 Maximum
    2 LiteralName
    2 ListenerOption
    2 Line
    2 JobName
    2 InFile
    2 Index
    2 IdleTimeoutSec
    2 Headers
    2 Header
    2 Guid
    2 FormatTypeName
    2 FormatsToProcess
    2 First
    2 Expression
    2 EventIdentifier
    2 EntryType
    2 End
    2 Drive
    2 DisplayHint
    2 DisableNameChecking
    2 DisableKeepAlive
    2 Delay
    2 Debugger
    2 Date
    2 Copyright
    2 ContentType
    2 ConfigurationTypeName
    2 CompanyName
    2 CommandName
    2 Column
    2 CimSession
    2 CimResourceUri
    2 CimNamespace
    2 CanonicalName
    2 Before
    2 AutoSize
    2 Author
    2 AsCustomObject
    2 ApplicationBase
    2 AppendPath
    2 AllowClobber
    2 All
    2 Alias
    2 After
    2 AccessMode

好了,看到这个列表,我相信大家对如何设置一个参数名称有了一定的了解了。参照这个列表上的名称,或者就使用这个列表上的名称,让你的cmdlet对更易懂!

关于PowerShell函数的参数应该怎样命名,本文就介绍这么多,希望对您有所帮助,谢谢!

更多精彩内容其他人还在看

Powershell学习笔记--使用正则表达式查找文件

本文介绍PowerShell中使用正则表达式的查找文件的方法,PowerShell的正则表达式与微软其它语言的正则表达式是一样的,使用非常方便。
收藏 0 赞 0 分享

Windows Powershell 介绍和安装

Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。
收藏 0 赞 0 分享

Windows Powershell 自定义控制台

这篇文章主要介绍了Windows Powershell 自定义控制台,包括选项、字体、布局和颜色四个方面的自定义风格,希望对大家有所帮助
收藏 0 赞 0 分享

windows Powershell 快速编辑模式和标准模式

powershell控制台有两种模式,一个是快速编辑模式,一个是标准模式。
收藏 0 赞 0 分享

PowerShell中查找字符串位置的IndexOf函数使用实例

这篇文章主要介绍了PowerShell中查找字符串位置的IndexOf函数使用实例,例子简单明了,容易看懂,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell使用正则表达式查找字符串实例

这篇文章主要介绍了PowerShell使用正则表达式查找字符串实例,主要是对match运算符的使用介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中使用正则表达式跨行匹配字符串的方法

这篇文章主要介绍了PowerShell中使用正则表达式跨行匹配字符串的方法,重点在于正则表达式的写法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中查看当前版本、Windows版本、.NET版本信息的代码

这篇文章主要介绍了PowerShell中查看当前版本、Windows版本、.NET版本信息的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 入门基础教程

Windows PowerShell 是专为系统管理员设计的新 Windows 命令行外壳程序。该外壳程序包括交互式提示和脚本环境,两者既可以独立使用也可以组合使用
收藏 0 赞 0 分享

PowerShell Contains函数查找字符串实例

这篇文章主要介绍了PowerShell Contains函数查找字符串实例,Contains函数的作用是查询一个字符串中是否存在另一个字符串,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多