What is the naming convention for Powershell funct

2019-01-26 07:01发布

问题:

While I did find a lot of information on how to name Cmdlets and functions in the Cmdlet Development Guidelines I did not find any information on whether functions should be named in upper or in lower case.

What is the convention here?

(I do unterstand that Cmdlets themselves are generally named in upper case, even though they are not case-sensitive when it comes to executing.)

回答1:

Naming convention can be tricky. While a fixed naming convention may provide aesthetics or simplified usage, it is not required to be followed. In general, a naming convention that I advocate for is the one that is used already in Powershell. As functions are created on verb-noun base, each word starts with a capital letter or if it is an abbreviation - all capitals, or if it is a proprietary - then as it is accordingly.

I have, for example, created some functions for myself:

Get-ServerDiag
Mount-TrueCryptVolumes
Start-RDP
Generate-RandomPassword
Nuke-Environment

You can imagine what these functions do, it is rather clear, straightforward and compliant with built-in Powershell functions. I do however have exceptions which come from "importing" a several Unix commands to Powershell (like killall, pidof etc...) You can always use a Set-Alias if you prefer to write something else.

This question, however important, is discussable as there does not seem to be the 'one, best way'. It is all, in the end, up to personal preferences.



回答2:

A function is mostly a script-based cmdlet. A cmdlet is written in ex. C#, while a function is written as a script. Because of this similarity, I recommend using the same style as cmdlets for my "standalone-functions" so that it blends in with the other PowerShell cmdlets. Ex. I had a filecount(per folder)-function that I used often. I called it Get-FileCount.

However, I usually name helper functions(functions you only use in other functions) using a simpler name like convertsidtousername etc.

You could use aliases to create short names for a function.