8

I have a PowerShell function Sort-VersionLabels. When I add this function to a module, Import-Module complains:

WARNING: Some imported command names include unapproved verbs which might make 
them less discoverable.  Use the Verbose parameter for more detail or type
Get-Verb to see the list of approved verbs.

According to this, Sort is a "reserved verb".

What could be a good (and approved) alternative?

Update
The function takes a array of version numbers in the form: <major>.<minor>.<revision>[-<milestone[nr]>]. Milestone can be dev, alpha, beta or stable (in that order). So the standard Sort-Object function won't work.

It outputs the sorted array to the pipe line.

Alan McBee
  • 4,202
  • 3
  • 33
  • 38
mhu
  • 17,720
  • 10
  • 62
  • 93
  • 1
    That verb is associated to `Group`. I get where the OP is coming from in trying to conform to standards but this is an odd one. The good synonyms of Sort are already used for other functionalities – Matt Nov 27 '14 at 15:14
  • I just meant that verbs like Set and Group are synonyms for Sort but in PowerShell they already have other meanings. Arrange could be used but it is flagged because of its association to Group. Order/Organize would be great but they are not approved. – Matt Nov 27 '14 at 16:56
  • I get what you mean now. – mhu Nov 27 '14 at 16:57
  • Check my updated comment. I am not sure if you will find an approved verb for this. You will most likely need to step out of that list to prevent being obscure or misleading – Matt Nov 27 '14 at 16:59
  • I guess it is ok to use the verb `show` here if this produce some output – Loïc MICHEL Nov 28 '14 at 06:22
  • 1
    FYI Have you seen this question about casting the string to `[system.version]`? http://stackoverflow.com/questions/711107/sorting-powershell-version – Matt Nov 29 '14 at 19:58
  • I was going to ask the same question. This seems to work for me `[Version] "1.0.0.1",[Version]"2.1.1",[Version]"2.3",[Version]"9.9.0.9" |sort -Descending ` – Micky Balladelli Nov 29 '14 at 20:17
  • Yes, the function I've written uses `[version]` for sorting the `..` part, but the milestone can `dev`, `alpha`, `beta` or `stable` in **that** order, which isn't alphabetical. – mhu Nov 29 '14 at 21:06

4 Answers4

8

I think something like ConvertTo-SortedVersionLabels, while a little bit awkward, uses an approved and non-reserved verb but is still clear.

You could also make sorting a parameter to a different function, like Get-VersionLabels -Sorted.

How you would work that in depends on your module as a whole and whether you have such a function to modify. It's unclear from your current post, but if you edit it with more details we might be able to provide more suggestions.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • I've added some additional info – mhu Nov 28 '14 at 13:42
  • 1
    I have settled on @briantist's suggestion _plus_ an alias. So OP's example would result in `function ConvertTo-SortedVersionLabels{}` _and_ an alias called `Sort-VersionLabels`. That seems to be the best compromise. – alx9r Nov 11 '15 at 19:58
6

The core of this issue will generate opinionated results. This creates a conundrum since you are looking for something specific that the current answers have been unable to address. I understand that you are looking for a solution that logically fits your function while being in the standard verb list, which is admirable. To continue from an earlier comment I made I am going to try and state a case for all the approved verbs that might fit your situation. I will refer to the Approved Verbs List linked in your question frequently and will use "AVL" for brevity going forward.

  1. Group: The comments on the AVL refers to using this in place of Arrange. Arrange being a synonym for Sort would be a good fit. Sticking with the recommendation then we should use Group
  2. Set: It is a synonym for Sort. However, in the AVL, it is associated with Write, Reset, Assign, or Configure which are not related to your cmdlet. Still, it is in the list and could fit if you are willing to put aside the discombobulation that it creates with existing PowerShell cmdlets.
  3. I dont really have a number 3.
  4. Update: This is a weak case but the AVL refers its use as a way to maintain [a cmdlets] state [and] accuracy.
  5. Order/Organize: Not in the AVL but I find these very fitting and dont currently conflict with any existing verbs.

Ultimately, AVL be damned and do whatever you want. Sort is a very good fit for what you are trying to do. You can also just use -DisableNameChecking when importing your module. It is only a warning after all. Briatist's answer is also good in my opinion.

Bonus from comments

Not that you asked for it, but when you said we have to enable name checking I thought about this. Just for fun!

$reservedVerbs = "ForEach","Format","Group","Sort","Tee"
$approvedVerbList = (Get-Verb).Verb

Get-Command -Module  Microsoft.WSMan.Management | ForEach-Object{
    If ($approvedVerbList -notcontains ($_.Name -split "-")[0]){
        Write-Warning "$($_.Name) does not use an approved verb."
    }

    If ($reservedVerbs -contains ($_.Name -split "-")[0]){
        Write-Warning "$($_.Name) is using a reserved verb."
    }
}
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Great answer. I think I'll stick with 'Sort'. Just means we have to enable name checking for our common modules once in awhile to check if a cmdlet with a non-AVL verb slipped through. Also you're English is definitely better than mine. – mhu Nov 30 '14 at 13:29
3

Whenever I need a verb that is not an approved PowerShell verb, I use Invoke-* instead. So in your case, you could name it Invoke-SortVersionLabels

ojk
  • 2,502
  • 15
  • 17
0

You shouldn't need a special cmdlet at all. If a VersionLabel is an object, just take the collection and pipe it to Sort-Object using the property(ies) you need.

# Assuming a versionlabel has a 'Name' Property...
$VersionLabelCollection | Sort-Object -Property:Name
Eris
  • 7,378
  • 1
  • 30
  • 45
  • 2
    It uses a custom comparer, which is not easily supported by Sort-Object (I could create a new type with IComparable support and use that, but that's a lot of work). – mhu Nov 28 '14 at 08:12