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定義函數參數的2種方法和傳參方法實例
- PowerShell函數參數指定數據類型實例
- PowerShell中給函數參數設置幫助信息的例子
- PowerShell函數參數用星號隱藏的方法
- PowerShell函數參數設置成自動識別數據類型的例子
- PowerShell函數參數設置為即可選又必選的方法
- PowerShell函數參數使用智能提示功能例子
- PowerShell使用枚舉變量定義帶智能提示功能的函數參數