Rules/Test-ForUnusableFunction.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
function Test-ForUnusableFunction 
{
    #region ScriptTokenValidation Parameter Statement
    param(
    <#
    This parameter will contain the tokens in the script, and will be automatically
    provided when this command is run within ScriptCop.
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    [Management.Automation.PSToken[]]
    $ScriptToken,
    
    <#
    This parameter will contain the command that was tokenized, and will be automatically
    provided when this command is run within ScriptCop.
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [Management.Automation.CommandInfo]
    $ScriptTokenCommand,
    
    <#
    This parameter contains the raw text of the script, and will be automatically
    provided when this command is run within ScriptCop
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]
    $ScriptText
    )
    #endregion ScriptTokenValidation Parameter Statement
    
    
    process {              
        $hasWriteHost = $ScriptToken |
            Where-Object { $_.Type -eq "Command" -and $_.Content -eq "Write-Host" }
        
        if ($hasWriteHost) {
            Write-Error "$ScriptTokenCommand uses Write-Host. Write-Host makes your scripts unsuable inside of other scripts. If you need to add tracing, consider Write-Verbose and Write-Debug"            
        }
        
        $hasReadHost = $ScriptToken |
            Where-Object { $_.Type -eq "Command" -and $_.Content -eq "Read-Host" }
        
        if ($hasReadHost) {
            Write-Error "$ScriptTokenCommand uses Read-Host. Read-Host makes your scripts unsuable inside of other scripts, because it means part of your script cannot be controlled by parameters. If you need to prompt for a value, you should create a mandatory parameter."            
        }
        
        
        $hasConsoleAPI = $ScriptToken |
            Where-Object { $_.Type -eq "Type" -and 
                ($_.Content -eq "Console" -or
                $_.Content -eq "System.Console")
                }

        if ($hasConsoleAPI) {
            Write-Error "$ScriptTokenCommand uses the console API. Using the console API ensures your script will only work in PowerShell.exe."            
        }

    }
}