Register-ScriptCopRule.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function Register-ScriptCopRule
{
    <#
    .Synopsis
        Registers a new script cop rule
    .Description
        Registers a new script cop rule.
        You can define custom script cop rules and use this command to register them on the fly.
    .Link
        Unregister-ScriptCopRule
    .Example
        Register-ScriptCopRule -File .\ARuleScript.ps1
    .Example
        Register-ScriptCopRule -Command (Get-Command ARule)
    #>

    [CmdletBinding(DefaultParameterSetName='Command')]
    [OutputType([Nullable])]
    param(
    # The Rule command. To Get Commandinfo, use the results of Get-Command
    [Parameter(ParameterSetName='Command',
        Mandatory=$true,
        ValueFromPipeline=$true)]
    [Management.Automation.CommandInfo]
    $Rule,

    # A file containing a script cop rule.
    [Parameter(ParameterSetName='File',
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    [Alias('FullName')]
    [String]
    $File
    )

    begin {
        #declare rules structure if it doesn't exist
        if (-not $script:ScriptCopRules) {
            $script:ScriptCopRules = @{
                TestCommandInfo = @()
                TestCmdletInfo = @()
                TestScriptInfo = @()
                TestFunctionInfo = @()
                TestApplicationInfo=  @()
                TestModuleInfo = @()
                TestScriptToken = @()
                TestHelpContent = @()
            }
        }
    }

    process {
        if ($psCmdlet.ParameterSetName -eq 'Command') {
            #region Register command
            $Rule |
                Test-ScriptCopRule -ErrorVariable Issues |
                Out-Null

            if ($Issues) { return }

            $parameterSetNames = 'TestCommandInfo','TestCmdletInfo','TestScriptInfo',
                'TestFunctionInfo','TestApplicationInfo','TestModuleInfo',
                'TestScriptToken','TestHelpContent'

            $Rule.ParameterSets |
                Where-Object {
                    $parameterSetNames -contains $_.Name
                } |
                ForEach-Object {
                    $script:ScriptCopRules[$_.Name] += $Rule
                    $script:ScriptCopRules[$_.Name] =
                        @($script:ScriptCopRules[$_.Name] |
                            Select-Object -Unique)
                }

            Write-Debug ($scriptCopRules | Out-String)
            #endregion
        } elseif ($psCmdlet.ParameterSetName -eq 'File') {
            #region Register File
            $command = Get-Item $File |
                Select-Object -ExpandProperty Fullname |
                Get-Command
            if (-not $command) { return }
            $command | Register-ScriptCopRule
            #endregion
        }

        $script:ScriptCopRules = $scriptCopRules

    }
}