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

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函数的参数应该怎样命名,本文就介绍这么多,希望对您有所帮助,谢谢!