I'm trying to set the variable value inside the script method which I try to attach to an object using Add-Member. What am I doing wrong?
$a = "old value"; $obj = New-Object -TypeName "System.Object" Add-Member -InputObject $obj -Name Info -Value { param($msg) $a = $msg; } -MemberType ScriptMethod $obj.Info("123"); Write-Host "a = $a"; I expected output to be a = 123 but I see a = old value. I think the issue is related to scoping (inside the script method, $a must mean a different variable since the value is not maintained). How do I set the value of the $a variable that I already have?