Dynamical get a cached variable?

Hey all,

im trying to figure out how to get a $cache:variable by using another variable as the variablename
anyone know how to do that ? normally I would use the Get-Variable, as it can use a variable for the name part, but cache is not an accepted scope :slight_smile:

it dont seems to work with $cache:$Variable or any variant of that.
I am very close to just build the complete code part as a string and use Invoke-Expression.

but before I do that, I want to ask for an alternative path :slight_smile:

Good day!

Here is the way I have managed to reference a dynamic name within a loop.

To create the dataβ€”

foreach ($Server in $Masterlist){
      Set-Item -PSPath "Cache:$($server.name)background" -Value $cache:BGfail
}

Then to recall the data –

foreach ($Server in $servers) {

 if (($(Get-Item -pspath cache:$($server.name)background) -eq $Cache:BGpass) ) {
      #Do something
 }
 else {
     $Cache:STATICNAME= $Cache:BGfail
 }

}

Also see post Auto-generated Variable names in $Cache: Scope - #2 by OpsEng

1 Like