I have below piece of code , where I am trying to access a variable defined in try but it's not accessible , what could be done to fix it.
try{
$X = get-X
}
catch
{
Manage exception
}
write-host $X
I just ran into this and it looks like a bug. Declaring the variable outside try block does not solve the scope issue. As soon you leave the try block value of $a gets reset to what it was before you entered try block. so if you want to access $a after executing try{} you need to access it as $global:a inside the try {} block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With