Get-ScriptCopFixer.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
function Get-ScriptCopFixer
{
    <#
    .Synopsis
        Gets all of the script cop fixers.
    .Description
        Gets all of the script cop fixers, and the relative path to the file defining the rule
    .Example
        Get-ScriptCopFixer
    .Link
        Repair-Command
    #>

    param()
    
    begin {
        # Declare the cache if fixers, if it doesn't exist yet.
        if (-not $script:ScriptCopFixers) {
            $script:ScriptCopFixers = New-Object Collections.ArrayList
        }
    }
    
    process {
        #region Convert Fixers from memory represention to psuedo object
        foreach ($fixer in @($script:ScriptCopFixers)) { 
            if ($fixer) {
                # Use Select-Object to turn it into the property bafe we want, and then add a typename
                $fixer |
                    Select-Object Name, @{
                        Label='File'
                        Expression={
                            if ($_.Path){ $_.Path.Replace("$psScriptRoot\", "") } else { $_.ScriptBlock.File.Replace("$psScriptRoot\", "") } 
                            
                        }
                    } | 
                    ForEach-Object {
                        $_.psObject.typenames.clear()
                        $null = $_.psObject.typenames.Add('ScriptcopRule')
                        $_
                    } 
            }
        } 
        #endregion
    }
}