Global variable with New-UDStep

Hi, I’m having an issue with global scope in my UD stepper. Updating the global variable within New-UDStepper script block works, but it doesn’t work when called from within the New-UDStep script block. It doesn’t get incremented.

Does anyone know if this is expected behavior or I’m doing something wrong?

Product: PowerShell Universal
Version: 2.8.0
````$global:test = 1
New-UDStepper -Steps {
    
    New-UDStep -OnLoad {
        New-UDElement -tag 'div' -Content { "Test  $global:test" }
    } -Label "Stage 1"
    $global:test += 1
        
    New-UDStep -OnLoad {
        New-UDElement -tag 'div' -Content { "Test $global:test" }
        $global:test += 1
    } -Label "Stage 2"
    
    New-UDStep -OnLoad {
        New-UDElement -tag 'div' -Content { "Test $global:test" }
    } -Label "Stage 3"
    
} -OnFinish {
    
} -OnValidateStep {
    $Context = $EventData

    if ($Context.CurrentStep -eq 0) {
        New-UDValidationResult -Valid        
    }
    
    if ($Context.CurrentStep -eq 1) {
        New-UDValidationResult -Valid        
    }
}`

This is because we use a runspace pool and there isn’t a guarantee the same runspace will be used for each OnLoad call.

Try using the $Cache scope instead.

$Cache:Test += 1

Alright makes sense, thanks for the answer