In this piece of code I'm getting a "UseDeclaredVarsMoreThanAssignments" warning from PSScriptAnalyzer
$lastUpdate = $false;
$shouldUpdate = $false;
if (Test-Path $logfile) {
(Get-Content $logfile) | Where-Object {
$_ -match "([^-]*)->\s*UpdateCheck"
} | ForEach-Object {
$lastUpdate = $Matches[1]
}
if ($lastUpdate) {
$shouldUpdate = (Get-Date $Matches[1]) -lt ((Get-Date).AddDays(-7))
} else {
$shouldUpdate = $true;
}
}
The variable that triggers the warning is $lastUpdate
in this line:
$lastUpdate = $Matches[1]
despite the fact that it's used int the next line.
Is this a problem with my code - I'm guessing it has something to do with scope and the ForEach-Object
loop - or is the problem with PSScriptAnalyzer in VSCode?