Test-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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function Test-ScriptCopRule
{
    [CmdletBinding(DefaultParameterSetName='TestCommandInfo')]
    param(
    [Parameter(ParameterSetName='TestCommandInfo',Mandatory=$true,ValueFromPipeline=$true)]
    [Management.Automation.CommandInfo]
    $CommandInfo
    )
    
    process {
        <#
         
        Only 3 types of commands can possibly be ScriptCopRules:
         
        - FunctionInfo
        - CmdletInfo
        - ExternalScriptInfo
         
        #>

        
        
        if ($CommandInfo -isnot [Management.Automation.FunctionInfo] -and
            $CommandInfo -isnot [Management.Automation.CmdletInfo] -and
            $CommandInfo -isnot [Management.Automation.ExternalScriptInfo]
        ) {
            Write-Error "$CommandInfo is not a function, cmdlet, or script" 
            return        
        }
        
        
        <#
         
        The parameter sets must have a specific name.
         
        The commands may have more than one parameter set.
         
         
        These parameter sets indicate the command can find problems
         
        - TestCommandInfo
        - TestCmdletInfo
        - TestScriptInfo
        - TestFunctionInfo
        - TestApplicationInfo
        - TestModuleInfo
        - TestScriptToken
        - TestHelpContent
         
        These parameter sets indicate the command can fix problems
         
        - RepairScriptCop
        #>

        
        
        $parameterSetNames = 'TestCommandInfo','TestCmdletInfo','TestScriptInfo',
            'TestFunctionInfo','TestApplicationInfo','TestModuleInfo',
            'TestScriptToken','TestHelpContent'
            
        $matchingParameterSets = $CommandInfo.ParameterSets |
            Where-Object {
                $parameterSetNames -contains $_.Name
            }
            
        if (-not $matchingParameterSets) {
            $ofs = ", " 
            Write-Error "$CommandInfo could not be a script cop rule because it does not have any of the correct parameter sets:
$parameterSetNames
"
            
            return
        }
        
        foreach ($matchingParameterSet in $matchingParameterSets) {
            switch ($matchingParameterSet.Name) 
            {
                TestCommandInfo {
                    $hasCommandParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'CommandInfo' -and
                        $_.ParameterType -eq [Management.Automation.CommandInfo]
                    }
                    if (-not $hasCommandParameter) {
                        Write-Error 'The TestCommandInfo parameter set does not have a CommandInfo parameter, or it is not the correct type'
                        return
                    }
                }
                
                TestFunctionInfo {
                    $hasFunctionParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'FunctionInfo' -and
                        $_.ParameterType -eq [Management.Automation.FunctionInfo]
                    }
                    if (-not $hasFunctionParameter) {
                        Write-Error 'The TestFunctionInfo parameter set does not have a FunctionInfo parameter, or it is not the correct type'
                        return
                    }
                }

                TestModuleInfo {
                    $hasModuleParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'ModuleInfo' -and
                        $_.ParameterType -eq [Management.Automation.PSModuleInfo]
                    }
                    if (-not $hasModuleParameter) {
                        Write-Error 'The TestModuleInfo parameter set does not have a ModuleInfo parameter, or it is not the correct type'
                        return
                    }
                }

                TestCmdletInfo {
                    $hasCmdletParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'CmdletInfo' -and
                        $_.ParameterType -eq [Management.Automation.CmdletInfo]
                    }
                    if (-not $hasCmdletParameter) {
                        Write-Error 'The TestCmdletInfo parameter set does not have a CmdletInfo parameter, or it is not the correct type'
                        return
                    }
                }
                
                TestApplicationInfo {
                    $hasApplicationParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'ApplicationInfo' -and
                        $_.ParameterType -eq [Management.Automation.ApplicationInfo]
                    }
                    if (-not $hasApplicationParameter) {
                        Write-Error 'The TestApplicationInfo parameter set does not have a ApplicationInfo parameter, or it is not the correct type'
                        return
                    }
                }

                TestScriptInfo {
                    $hasScriptParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'ScriptInfo' -and
                        $_.ParameterType -eq [Management.Automation.ExternalScriptInfo]
                    }
                    if (-not $hasScriptParameter) {
                        Write-Error 'The TestScriptInfo parameter set does not have a ScriptInfo parameter, or it is not the correct type'
                        return
                    }
                }

                TestHelpContent {
                    $hasScriptParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'HelpContent'
                    }
                    if (-not $hasScriptParameter) {
                        Write-Error 'The TestHelpContent parameter set does not have a HelpContent parameter, or it is not the correct type'
                        return
                    }
                }

                TestScriptToken {
                    $hasCommandParameter = $matchingParameterSet.Parameters | Where-Object {
                        $_.Name -eq 'ScriptToken' -and
                        $_.ParameterType -eq [Management.Automation.PSToken[]]
                    }
                    if (-not $hasCommandParameter) {
                        Write-Error 'The TestScriptToken parameter set does not have a ScriptToken parameter, or it is not the correct type'
                        return
                    }
                }            
            }
        }
    }
}