PowerShell / PowerShell/PSScriptAnalyzer
PSAvoidAssignmentToAutomaticVariable generates false positives during script analysis
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C#
- Sterne
- 2.2k
- Forks
- 414
- Ø Merge
- 13 Std. 1 Min.
- Gemergte PRs (30 T.)
- 2
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die bereitgestellte Write-Log-Funktion durch PSScriptAnalyzer laufen zu lassen und die Regel PSAvoidAssignmentToAutomaticVariable zu untersuchen, die den Parameter $Error meldet. Bestätige die Diagnose anhand des angegebenen PowerShell-Scoping-Verhaltens; erledigt ist dies, wenn dieser Fall nicht gemeldet wird und der Diagnose-Text korrigiert wird, falls er andernorts weiterhin zutrifft.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- csharp, powershell
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100