Resolved: ThreadJob in powershell is not working with global variables

In this post, we will see how to resolve ThreadJob in powershell is not working with global variables

Question:

I have the below code where myValues will be updated by multiple functions (in this file) and I want to capture that change and process myValues in a different thread so that the current execution of updating myValues is not stopped. But logsToDashboard is not event getting called in here. Not sure what am i missing. Would appreciate any leads.

Best Answer:

You need to use the using: scope modifier to reference a local a variable in your new thread. Also using global: is pointless in your code.
Another way to do it is with -ArgumentList and, you can but not mandatory, add a param block to your function. Without the param block you would refer to the list using $args[0] instead of $list.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com