Rules/Test-CommandNamingConvention.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Test-CommandNamingConvention
{
    param(
    [Parameter(ParameterSetName='TestCommandInfo',Mandatory=$true,ValueFromPipeline=$true)]
    [Management.Automation.CommandInfo]
    $CommandInfo
    )
    
    begin {
        
        $standardVerbs = @{}
        Get-Verb | 
            ForEach-Object { $standardVerbs."$($_.Verb)" = $_ } 
    }
    
    process {    
        $commandName = $commandInfo.Name
        $verb, $noun, $rest = $commandName -split '-'
        if (-not $noun) {
            Write-Error "$CommandInfo does not follow the verb-noun naming convention.
PowerShell commands should be named like:
 
StandardVerb-CustomNoun
 
To see all of the standard verbs, run 'Get-Verb'
"

            return
        }
        
        if ($rest) {
            Write-Error "$CommandInfo name contains an additional -, please rename the command"
            return
        }
        
        if (-not $standardVerbs.$verb) {
            Write-Error "$CommandInfo uses a non-standard verb, $Verb. Please change it. Use Get-Verb to see all verbs"
        }
        
        if ($commandName.IndexOfAny("#,(){}[]&/\`$^;:`"'<>|?@``*%+=~ ".ToCharArray()) -ne -1)
        {
            Write-Error "$commandInfo name contains invalid characters"
        }
        
    }
}