What are good guidelines for naming PowerShell ver

2019-04-10 02:10发布

问题:

I'm early on in my PowerShell learning, and I'm wondering if there are some good guidelines for verbs in Posh for cmdlets (or advanced functions, whatever they're called in CTP3).

If I do a get-verb I can see the lot of them. But I'm still not sure how I should lay out my modules.

Here's the example I'm running into right now. I have a little script that asks Perforce: if I were to sync, what files would change and how big are they? It outputs a summary of sizes and a mini-tree of folders for where the changes will occur (as well as how many would need resolving).

Is that a query-p4sync? Or is it a 'sync-p4 -whatif'? Or something else?

Before I start writing a lot of these scripts I want to make sure I name them right.

回答1:

You can find a list of common verbs on MSDN along with a description what they should be used for.



回答2:

Here's an updated list of approved verbs on the Windows PowerShell Blog, as of July 15.



回答3:

From your use of the word "modules", I'm going to guess you are using V2 of PowerShell, which allows you to take advantage of Advanced Functions.

Advanced functions provide a way to attribute your function to provide native support for -WhatIf and -Confirm

function Sync-PerforceRepository()
{
  [cmdletbinding(SupportShouldProcess=$true)]
  param (...) #add your parameters
  Begin
  {
    #setup code here
  }
  Process
  {
    if ($pscmdlet.ShouldProcess($ObjectBeingProcessed,"String Describing Action Happening")
    {
      #Process logic here
    }
  }
  End
  {
     #Cleanup code
  }

}