-
Notifications
You must be signed in to change notification settings - Fork 375
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
Labels
Comments
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 ? |
What about simply accepting an object (instead of a string) and stringify ( 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
Summary of the new feature
[ScriptBlock]
s might contain both single and double quotes therefore it would makes sense to accept aScriptBlock
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)
(Workaround)
What is the latest version of PSScriptAnalyzer at the point of writing
1.21.0
The text was updated successfully, but these errors were encountered: