PowerShell中对函数参数的命名建议
PowerShell自定义函数的参数没有具体的命名规则,那么怎么做才能让参数的命名更加科学可持续。
我们应该怎么来命名函数的参数名称呢?有没有一个可供参考的列表?当然微软没有给出来。但是我们可以来理一个列表出来。微软自带了大量的cmdlet,这些cmdlet使用的参数有什么规律呢?我们用下面一段命令来统计一下。
Get-Command-CommandTypeCmdlet| ForEach-Object{$_.Parameters}| ForEach-Object{$_.Keys}| Group-Object-NoElement| Sort-ObjectCount,Name-Descending| Select-Object-Skip11| Where-Object{$_.Count-gt1}
这段代码的作用就是把所有的cmdlet使用的参数名称来做一个统计,看看哪些名称的参数用得比较多,然后按从多到少的顺序做一个列表。小编这边运行后的结果如下:
CountName --------- 69Force 56InputObject 53Credential 52Path 48PassThru 43LiteralPath 35UseTransaction 33Exclude 32Include 30ComputerName 28Filter 26Id 14Scope 14Property 14InstanceId 13Value 13Authentication 12ThrottleLimit 12Description 12ArgumentList 10Session 10Encoding 9SourceIdentifier 8Wait 8Namespace 8DisplayName 8CertificateThumbprint 8AsJob 7UseSSL 7Stream 7State 7Port 7NoClobber 7Impersonation 7FilePath 6TypeName 6Timeout 6SessionOption 6Option 6Module 6Message 6LogName 6Job 6ConnectionUri 6ConfigurationName 6Command 6Class 6ApplicationName 6AllowRedirection 5View 5Variable 5Source 5Recurse 5PSProvider 5Delimiter 4UseDefaultCredentials 4UseCulture 4UICulture 4StackName 4ShowError 4NewName 4MessageData 4Locale 4GroupBy 4Expand 4EnableAllPrivileges 4DisplayError 4Destination 4Depth 4Culture 4Count 4Certificate 4Category 4CaseSensitive 4Body 4Authority 4Append 4Action 3Width 3Uri 3SupportEvent 3Server 3SecurityDescriptorSddl 3ScriptBlock 3Restart 3PSVersion 3ProxyCredential 3OutputBufferingMode 3NoTypeInformation 3NoServiceRestart 3Newest 3ModulesToImport 3MemberType 3MaxTriggerCount 3MaximumRedirection 3LocalCredential 3List 3Function 3Forward 3EnableNetworkAccess 3CommandType 3Cmdlet 3Breakpoint 3AsString 3AssemblyName 3As 2WebSession 2Visibility 2Verb 2UseSharedProcess 2UserAgent 2UnjoinDomainCredential 2Unique 2TypesToProcess 2TypeData 2Type 2TransportOption 2TransferEncoding 2TotalCount 2Title 2TimeoutSec 2ThreadOptions 2ThreadApartmentState 2SubscriptionId 2Strict 2Status 2StartupType 2StartupScript 2SkipNetworkProfileCheck 2Skip 2ShowSecurityDescriptorUI 2SessionVariable 2SessionTypeOption 2SessionType 2SecondValue 2Seconds 2ScriptsToProcess 2Script 2RunAsCredential 2RestorePoint 2Resolve 2Quiet 2Query 2PSSession 2PSHost 2ProxyUseDefaultCredent... 2Proxy 2ProcessorArchitecture 2PrependPath 2Prefix 2PowerShellVersion 2OutFile 2Off 2NotMatch 2ModuleInfo 2Minimum 2Method 2MemberName 2MaximumReceivedObjectS... 2MaximumReceivedDataSiz... 2Maximum 2LiteralName 2ListenerOption 2Line 2JobName 2InFile 2Index 2IdleTimeoutSec 2Headers 2Header 2Guid 2FormatTypeName 2FormatsToProcess 2First 2Expression 2EventIdentifier 2EntryType 2End 2Drive 2DisplayHint 2DisableNameChecking 2DisableKeepAlive 2Delay 2Debugger 2Date 2Copyright 2ContentType 2ConfigurationTypeName 2CompanyName 2CommandName 2Column 2CimSession 2CimResourceUri 2CimNamespace 2CanonicalName 2Before 2AutoSize 2Author 2AsCustomObject 2ApplicationBase 2AppendPath 2AllowClobber 2All 2Alias 2After 2AccessMode
好了,看到这个列表,我相信大家对如何设置一个参数名称有了一定的了解了。参照这个列表上的名称,或者就使用这个列表上的名称,让你的cmdlet对更易懂!
关于PowerShell函数的参数应该怎样命名,本文就介绍这么多,希望对您有所帮助,谢谢!