-
Notifications
You must be signed in to change notification settings - Fork 395
Rule PSAvoidShouldContinueWithoutForce
doesn't enforce correct use of the Force
parameter
#1308
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
Comments
Question: Does anyone have a preference on which line is flagged as in violation?
i.e. as it's the combination of these three, plus the lack of the use of the force parameter further on which cause the issue. My instinct is to attach the violation to the |
Note: All of the following scenarios should be deemed valid: function Invoke-MVP {
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType('System.String')]
Param (
[Parameter()]
[switch]$Force
)
if ($Force -or $PSCmdlet.ShouldContinue('Should I continue','Should I continue')) {
'I continued'
}
} function Invoke-MVP {
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType('System.String')]
Param (
[Parameter(Mandatory = $true)]
[string[]]$ComputerName
,
[Parameter()]
[switch]$Force
)
[bool]$yesToAll = $Force
[bool]$noToAll = $false
foreach ($computer in $ComputerName) {
if ($PSCmdlet.ShouldContinue('Should I continue','Should I continue', [ref]$yesToAll, [ref]$noToAll)) {
"I continued: $computer"
}
}
} In theory the below is also valid... but I suspect this is considered bad practice so should be reported as a violation / up to the coder to hide this via a function Invoke-MVP {
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType('System.String')]
Param (
[Parameter()]
[switch]$Force
)
if ($Force) {
'I continued... She made me do it!'
} else {
if ($PSCmdlet.ShouldContinue('Should I continue','Should I continue')) {
'I continued'
}
}
} Note: I wouldn't have it treat |
I'd flag on |
If you declare a parameter and then never use it, should you get a I know this is an issue about specifically using a |
That rule doesn't currently look at param blocks, but I think this is a good idea for a new rule. I have a POC of it that works so far but doesn't cover cases like |
Currently
PSScriptAnalyzer
shows aPSAvoidShouldContinueWithoutForce
violation for any scripts using$PSCmdlet.ShouldContinue
that do not include a$Force
parameter.However, once the parameter the violation is resolved; even if that parameter is never used elsewhere in the code.
Steps to reproduce
Expected behavior
A violation of rule
PSAvoidShouldContinueWithoutForce
should occur.Actual behavior
The code is deemed free of violations.
Environment data
The text was updated successfully, but these errors were encountered: