4
$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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • 2
    there is an issue over at the PSScriptAnalyzer site about this ... i think it has to do with scoping. the rule is not recognizing the scope modifier - and you are using the $Var in more than one scope. ///// as an aside, what you are doing is a bad idea and should be avoided if at all possible. there should be ONE way into a function - the parameters - and ONE way out - the return [or equivalent]. ///// also, the `()` in your function calls is both unneeded and misleading. the last is because it implies that `FuncName (parameter1, parameter2)` is a legit way to call a function. it is NOT. – Lee_Dailey Jul 20 '19 at 16:01

1 Answers1

17

Easy, don't use global variables!

Although, if you must, you can suppress PSSA warnings with SuppressMessageAttribute:

function RunSecund(){
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope='Function')]
    $global:ProjectName = "a name"
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206