I don’t have any grand reveal or workaround here, just noting something I encountered yesterday and didn’t find anything else on the Internet regarding it.
PowerShell has scopes for variables. I like to use the Global and Script scopes and while they usually work, I realized they don’t in Azure Functions. The only scope that seems to work there is the Env:
scope. The closest I could find on this was this issue which didn’t have any solution.
Update (23rd August 2024): It looks like scope sort of work, as long as I am not using the scope modifier. That is to say, consider the code below:
1 2 3 4 5 6 7 8 9 10 |
$var1 = "1" function Func1 { Write-Host "Var1 is $var1" $scvar1 = $Script:var1 Write-Host "ScriptVar1 is $scvar1" } Func1 |
You expect to see $Script:var1
within the Function but it is not visible. Thankfully, calling the variable directly using the implicit scopes works.
That’s good, because Functions too can see each other and call each other. Like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$var1 = "1" function Func1 { Write-Host "Var1 is $var1" $scvar1 = $Script:var1 Write-Host "ScriptVar1 is $scvar1" } function Func2 { Write-Host "Func2" Write-Host "Calling Func1" Func1 } Func2 |
Because of the implicit scope, Func2
can see Func1
and call it.