What are good guidelines for naming PowerShell ver

2019-04-10 01:34发布

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.

3条回答
女痞
2楼-- · 2019-04-10 02:03

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

查看更多
Ridiculous、
3楼-- · 2019-04-10 02:12

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
  }

}
查看更多
Fickle 薄情
4楼-- · 2019-04-10 02:22

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

查看更多
登录 后发表回答