Saturday, January 7, 2012

Powershell (v2) - Getting Module Members

As I work through some module definitions on my Base.psm1 module I have encountered a few warnings,
PS > Import-Module Base
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
To try and figure out which verbs I was having issues with I went to the approved list of verbs and checked out each one to find the offenders using this command to list my basic command set,
PS > Get-Command -Module Base
CommandType     Name                                                Definition
-----------     ----                                                ----------
Function        Add-NetworkingHostsEntry                        ...
Function        Copy-IOFile                                     ...
Function        Copy-IOFolder                                   ...
Function        Create-IISAppPool                               ...
Function        Create-IOFile                                   ...
Function        Create-IOFolder                                 ...
Alias           ifum                                                Import-UtilityModules
Function        Import-UtilityModules                           ...
Function        Remove-IOFile                                   ...
Function        Remove-IOFolder                                 ...
Function        Test-IISAppPoolExists                           ...
Function        Test-IISAppPoolIsStarted                        ...
Function        Test-IOFileExists                               ...
Function        Test-IOFolderExists                             ...
Function        Validate-IISWebApplication                      ...
Alias           wfudt                                               Write-UtilityDateTime
Function        Write-UtilityDateTime                           ...
Once I had my command set I was able to check each verb out.
PS > get-verb create
PS > get-verb import
Verb                                                        Group
----                                                        -----
Import                                                      Data
PS > get-verb test
Verb                                                        Group
----                                                        -----
Test                                                        Diagnostic
PS > get-verb validate
PS > get-verb write
Verb                                                        Group
----                                                        -----
Write                                                       Communications
To automate this further, I used this approach:
# Clear screen
cls

# Variables
$validatedverbs = @{};

# Gather verbs
$verbs = Get-Command -Module Base -CommandType Function | `
ForEach-Object { $_.Name.ToString().Split("-")[0] } | `
Select-Object -Unique;

# Test verbs for validity
foreach($verb in $verbs) {
  if((Get-Verb -verb $verb) -eq $null) {
    $validatedverbs.Add($verb, $false);
  } else {
    $validatedverbs.Add($verb, $true);
  }
}

$validatedverbs.GetEnumerator() | Sort-Object -Property Key
When I run it I get this clear list of what functions are not on the common verb list,
Name                           Value                                                                                                              
----                           -----                                                                                                              
Add                            True                                                                                                                
Copy                           True                                                                                                                
Create                         False                                                                                                              
Import                         True                                                                                                                
Remove                         True                                                                                                                
Test                           True                                                                                                                
Validate                       False                                                                                                              
Wacky                          False                                                                                                              
Write                          True                                                                                                                

2 comments:

  1. Nice approach to finding illegal verbs being used in functions. Another best practice is to also use singular nouns instead of plural nouns.

    Ex. Import-UtilityModule instead of Import-UtilityModules

    Boe

    ReplyDelete
  2. Grr... I knew that one was out there too.

    ReplyDelete