How to fix 'Variable not used' warning in Powershell, when variable is actually used?

martorad

Here is the relevant code:

$Ports | ForEach-Object {
    $processID = $(Start-Process ".\nrfutil.exe" -ArgumentList "dfu usb-serial --package $path --port $_" -PassThru).Id
}
while ($(Get-Process -Id $processID).ProcessName) {}
Write-Host "DONE!" -ForegroundColor Green

and here is how VSCode highlights it: VSCode highlight Error message Is this happening because the interpreter doesn't know if $Ports will actually have any values in it, therefore the ForEach-Object loop may not run? If so, how to fix this highlight? The code works perfectly fine, I just want to remove the erroneous highlight.

I am using Microsoft's official VSCode Powershell support, version v2021.10.2

Patrick

I would rewrite the code and use a foreach loop.
Also it is possible to directly access ProcessName and you don't have to get the process again with the id.

foreach ($Port in $Ports)
{
    $process = Start-Process ".\nrfutil.exe" -ArgumentList "dfu usb-serial --package $path --port $Port" -PassThru
    $process.ProcessName
}

Write-Host "DONE!" -ForegroundColor Green

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SBCL warning that a variable is defined but never used

How to powershell to give warning or error when using an undefined variable?

Why ReactJs says "warning 'counter:' is defined but never used" but variable is used?

Unused lambda capture warning when capture is actually used

Eclipse missing warning if variable used before assigned

Why variable importance is not reflected in variable actually used in tree construction?

How to detect the variable when used?

PowerShell command fails after variable is used

Variable is not used

How to silence "variable not used" warning and maintain a strong reference?

How to store a variable to be used by a then promise?

How is this variable used in this ReactJs example

Fix "Variable is used before being assigned" error

XSLT discrepancy with how variable is used

declared but not used when reassigning variable

Variable 'self' was written to, but never read warning message when XLPagerTabStrip is used

How to expand filenames with Tab, even when a shell variable is used in the path?

How to set and retrieve ArrayList when used as a member variable of another object

Script break when variable is used

Pycharm warns "Local variable <variable> value is not used" in "if" block though it actually used

Index is not used when using a variable

C# Warning: Variable is assigned but never used

How are env variable stored and used?

How to handle exceptions when they are used to calculate another variable?

How to reuse a variable in shell script when used in curl?

how to fix cannot resolve symbol and variable never used, errors

How can an if statement change a variable, when there's no assignement operator used?

How to fix Error: Non-nullable variable to be used?

How do I fix " (Variable name here) is a variable but is used like a type?"