Chang variable value in Function
i have $a defined in function a, $b defined in function b , $c defined in main
in functions, i can use $script:c make change in $c. cannot use $script:a in function b make change in $a
any way that? thanks
function a() { $a = 1; $script:c = 4; b }
function b() { $b = 2; $script:a = 5 ? }
$c = 3
as long function called function b (as done in example), can using numbered scope. scope 1 immediate parent scope. however, need use set-variable command instead of $variable syntax make work. here's modification of sample code demonstrate this:
function functiona { $a = 1 functionb $script:c = $a } function functionb { $b = 2 set-variable -scope 1 -name -value $b } $c = 3 functiona $c
in general, it's cleaner pass values in functions arguments, , out on output stream (or via [ref] parameters), rather modifying variables in parent scope directly.
Windows Server > Windows PowerShell
Comments
Post a Comment