PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.8k stars 365 forks source link

Accept `[ScriptBlock]` for `-ScriptDefinition` #1937

Open iRon7 opened 10 months ago

iRon7 commented 10 months ago

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 commented 9 months ago

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 commented 9 months ago

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

Prototype ```PowerShell [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.