James O'Neill's Blog

December 2, 2008

PowerShell Verbs Vs Nouns

Filed under: Powershell,Real Time Collaboration — jamesone111 @ 11:54 am

The first big PowerShell project I worked on was to produce the scripts in the OCS resource Kit. With OCS R2 announced, it won’t come as a great surprised that we’re working on the Reskit again and I’ve gone back to my scripts. Boy oh Boy have I learnt some stuff in the last year.

  • PowerShell nouns are written in the singular. (I had a mix of singular and plural)
  • Be consistent with Nouns (don’t have “usage” in one place and “OCSPhoneRouteUsage” in another)
  • Avoid creating new verbs. (I’d written LIST-, when Get- would have done, LINK- for Add)
  • Try to allow the user to pipe things into commands, pass an object or a name to fetch the object
  • Allow Wildcards in names where possible. Allow people to use * even when if queries prefer %

The list goes on. I had already produced a table of verbs to nouns for the first release and I had this post of Jeffrey’s rattling round in my head. Rather than use his code I put my own together, using my new favourite PowerShell cmdlet, Select-String.

Function Get-VerbNounMatrix
{Param ($scriptName)

$Functions=Select-String -Pattern "^function|^filter" -path $scriptName | % {$_.line.split(" ")[1]}

$Verbs=($functions | forEach {$_.split("-")[0]} | sort -unique)

$Nouns=($functions | forEach {$_.split("-")[1]} | sort -unique)

$(foreach ($n in $Nouns){

$verbs | foreach -begin {$Info = New-Object -TypeName System.Object

Add-Member -inputObject $Info -MemberType NoteProperty -Name "Type" -Value $n} `

-process {Add-Member -inputObject $Info -MemberType NoteProperty -Name $_ `

-Value $(if ($functions -Contains "$_-$n") {"*"} else {" "})} `

-end {$info} }

) | export-csv -path $scriptName.toUpper().replace("PS1","CSV") }


The $functions= line gets all the lines in the specified file which start either “Function” or “Filter”, splits them where it finds space and takes the function name part (after the first space)

the $verbs= and $nouns= bit give the function arrays of the two halves of the function name, and then all the work is done in two nest loops

For each noun, it looks at each verb and creates an object with properties whose names match the verbs and whose values are set to a space or a * depending on whether the verb-noun combination exists; each object also gets a type property which is the noun. These object are then sent out to a CSV file.

and here’s the result (with a little cleaning up – the first time I ran it I found a function which was still using the wrong noun; not only is this is a great way of showing quickly what you have to others, but it shows you what you need to go back and fix).

Type Add Choose Export Get Import New Remove Update
ADUser     * *        
OCSADContainer       *        
OCS * Cert       *        
OCSEdgeFederationDenied       *   * *  
OCSEdgeFederationPartner     * * * * *  
OCSEdgeIMProvider       *   * * *
OCSEdgeInternalDomain       *   * *  
OCSEdgeInternalServer       *   * *  
OCSErrorEvent       *        
OCSGlobalUCSetting       *        
OCSInstalledService       *        
OCSLocationProfile   *   *   * *  
OCSMediationServer   *            
OCSMediationServerSetting       *        
OCSMeetingPolicy   *   *   * * *
OCSNormalizationRule   *   *   * * *
OCSPhoneRoute   *   *   * * *
OCSPhoneRouteUsage   *   *   * *  
OCSPICUserCount       *        
OCSPool   *   *        
OCSSchemaVersion       *        
OCSSIPDomain       *   * *  
OCSSipRoutingCert       *        
OCSTrustedService       *        
OCSUCPolicy   *   *   * * *
OCSUser     * * * * * *
OCSUserDetail       *        
OCSWarningEvent       *        
OCSWindowsService       *        


This post originally appeared on my technet blog.

Blog at WordPress.com.

%d bloggers like this: