Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept [ScriptBlock] for -ScriptDefinition #1937

Open
iRon7 opened this issue Aug 29, 2023 · 2 comments
Open

Accept [ScriptBlock] for -ScriptDefinition #1937

iRon7 opened this issue Aug 29, 2023 · 2 comments

Comments

@iRon7
Copy link

iRon7 commented Aug 29, 2023

Summary of the new feature

[ScriptBlock]s might contain both single and double quotes therefore it would makes sense to accept a ScriptBlock for the -ScriptDefinition parameter so that it isn't necessarily to replace either the single quotes or double quotes in e.g. (Pester) test scenario's.

Proposed technical implementation details (optional)

Invoke-ScriptAnalyzer -ScriptDefinition { Write-Host 'Test:' "$Test" }

(Workaround)

Invoke-ScriptAnalyzer -ScriptDefinition { Write-Host 'Test:' "$Test" }.ToString()

What is the latest version of PSScriptAnalyzer at the point of writing

1.21.0

@bergmeister
Copy link
Collaborator

Adding another parameter set is not worth the added complexity in my honest opinion. It's a nice but not necessary feature, therefore I'd lean towards closing, what do you think @JamesWTruher ?

@iRon7
Copy link
Author

iRon7 commented Sep 18, 2023

What about simply accepting an object (instead of a string) and stringify ($ScriptDefinition = [String]$ScriptDefinition that for the current -ScriptDefinition implementation?

Prototype
[CmdletBinding(DefaultParameterSetName='Path_SuppressedOnly', SupportsShouldProcess=$true, ConfirmImpact='Medium', HelpUri='https://go.microsoft.com/fwlink/?LinkId=525914')]
param(
    [Parameter(ParameterSetName='Path_IncludeSuppressed', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Parameter(ParameterSetName='Path_SuppressedOnly', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('PSPath')]
    [ValidateNotNull()]
    [string]
    ${Path},

    [Parameter(ParameterSetName='ScriptDefinition_IncludeSuppressed', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Parameter(ParameterSetName='ScriptDefinition_SuppressedOnly', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    ${ScriptDefinition},

    [Alias('CustomizedRulePath')]
    [ValidateNotNull()]
    [string[]]
    ${CustomRulePath},

    [switch]
    ${RecurseCustomRulePath},

    [switch]
    ${IncludeDefaultRules},

    [ValidateNotNull()]
    [string[]]
    ${ExcludeRule},

    [ValidateNotNull()]
    [string[]]
    ${IncludeRule},

    [ValidateSet('Warning','Error','Information','ParseError')]
    [string[]]
    ${Severity},

    [switch]
    ${Recurse},

    [Parameter(ParameterSetName='Path_SuppressedOnly')]
    [Parameter(ParameterSetName='ScriptDefinition_SuppressedOnly')]
    [switch]
    ${SuppressedOnly},

    [Parameter(ParameterSetName='Path_IncludeSuppressed', Mandatory=$true)]
    [Parameter(ParameterSetName='ScriptDefinition_IncludeSuppressed', Mandatory=$true)]
    [switch]
    ${IncludeSuppressed},

    [Parameter(ParameterSetName='Path_IncludeSuppressed')]
    [Parameter(ParameterSetName='Path_SuppressedOnly')]
    [switch]
    ${Fix},

    [switch]
    ${EnableExit},

    [Alias('Profile')]
    [ValidateNotNull()]
    [System.Object]
    ${Settings},

    [switch]
    ${SaveDscDependency},

    [switch]
    ${ReportSummary})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) { $PSBoundParameters['OutBuffer'] = 1 }
        if ($PSBoundParameters.TryGetValue('ScriptDefinition', [ref]$outBuffer)) { # https://github.com/PowerShell/PowerShell/issues/19949
            $PSBoundParameters['ScriptDefinition'] = [String]$ScriptDefinition
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('PSScriptAnalyzer\Invoke-ScriptAnalyzer', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }

        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        if ($ScriptDefinition) {
            $steppablePipeline.Process([String]$_)
        }
        else {
            $steppablePipeline.Process($_)
        }
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}

clean
{
    if ($null -ne $steppablePipeline) {
        $steppablePipeline.Clean()
    }
}
<#

.ForwardHelpTargetName PSScriptAnalyzer\Invoke-ScriptAnalyzer
.ForwardHelpCategory Cmdlet

#>
.\Invoke-ScriptAnalyzer.ps1 -ScriptDefinition { Write-Host 'Test:' "$Test" }

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidTrailingWhitespace           Information             1     Line has trailing whitespace
PSAvoidUsingWriteHost               Warning                 1     Script definition uses Write-Host. Avoid using Write-Host
                                                                  because it might not work in all hosts, does not work when
                                                                  there is no host, and (prior to PS 5.0) cannot be
                                                                  suppressed, captured, or redirected. Instead, use
                                                                  Write-Output, Write-Verbose, or Write-Information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants