Question:
1 2 3 4 5 6 7 8 9 10 11 |
$global:ProjectName = $null function RunFirst(){ RunSecund Write-Host $global:ProjectName } function RunSecund(){ $global:ProjectName = "a name" } |
In RunSecund I get:
The variable ‘ProjectName’ is assigned but never used. PSScriptAnalyzer(PSUseDeclaredVarsMoreThanAssignments)
Answer:
Easy, don’t use global variables!
Although, if you must, you can suppress PSSA warnings with SuppressMessageAttribute
:
1 2 3 4 5 |
function RunSecund(){ [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope='Function')] $global:ProjectName = "a name" } |