PowerShell / PowerShell/PSScriptAnalyzer
PSAvoidAssignmentToAutomaticVariable generates false positives during script analysis
Personne n'a encore pris cette issue.
- Langage dominant
- C#
- Étoiles
- 2.2k
- Forks
- 414
- Merge moyen
- 13 h 1 min
- PR mergées (30 j)
- 2
Description
Steps to reproduce
Run this function through PSSA.
function Write-Log {
[CmdletBinding()]
param
(
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Message,
[switch]
$Error
)
$callerEAP = $ErrorActionPreference
try {
$messageColor = if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Error') -and $Error) {
[ConsoleColor]::Red
} else {
[ConsoleColor]::Cyan
}
Write-Host -Foreground $messageColor -Object $Message
# Reset colors to return to default after an error message on a compilation error
[System.Console]::ResetColor()
} catch {
Write-Error -ErrorRecord $_ -ErrorAction $callerEAP
}
}
Expected behavior
It passes.
Actual behavior
PSSA reports the following:
The Variable 'Error' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.
There are a number of problems with this:
- The comma should be a period for it to read properly.
- The error text is incorrect.
$Errorcan be assigned in child scopes without issue.$Erroris read-only only in the global scope, and errors are automatically written into that globally-scoped collection that is read-only from PowerShell. In functions, or child scopes, you can use a$Errorvariable to your hearts content. - Most importantly: the issue that is being called out by PSSA here should not be called out at all.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.1
PSEdition Core
GitCommitId 7.0.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par exécuter la fonction Write-Log fournie via PSScriptAnalyzer et examinez la règle PSAvoidAssignmentToAutomaticVariable qui signale le paramètre $Error. Confirmez le diagnostic au regard du comportement de portée de PowerShell indiqué ; c’est terminé lorsque ce cas n’est pas signalé et que le texte du diagnostic est corrigé s’il reste applicable ailleurs.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- csharp, powershell
- Domaine
- tooling
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100