Multiple variables challenge

I will try to ask my question via sample code

$TeamsSettings = get-teams #I can run this line only once for some specific reasons
Function one{
foreach ($team in $TeamsSettings ){
$teamid= $team.Id
$settings = get-settings | {$_.teamid - $teamid}  # this can be run only once in a script
new-variable -Name "settings${teamid}" -Value $settings -Scope script #I need to use this variable in second function
#e.g for this team, settings are stored in variable $settings3838

Function two{
foreach ($team in $TeamsSettings ){
$teamid= $team.Id #I use this to determine second part of the variable name from first function to get right value for this team
 $settings${teamid}  # I need to get variable value from first function (e.g. $settings3838) for each team but I'm not sure how

Use a PS custom object:

$TeamsSettings = get-teams

$TeamInfo = foreach ($team in $TeamsSettings){
    New-Object -TypeName PSObject -Property ([Oredered]@{
        TeamId   = $team.Id
        Settings = get-settings | where { $_.teamid -eq $ } # this can be run only once in a script

foreach ($team in $TeamsSettings){
    $thisteam = $TeamInfo | where { $_.TeamId -eq $ }  # use this to determine the team based on its id
    $thisteam.Settings # Use the settings.. 

Or better yet, I would pick the desired properties from each of the 2 input objects (the one from get-teams and the one from get-settings), and create a PS custom object that is to be used for the rest of the script. Joining the 2 objects would be by the ‘id’ property as in:

$TeamDesiredProperyList     = @('bla1','bla2','bla3')
$SettingsDesiredProperyList = @('bla4','bla5','bla6')

$myBlendedTeamObjectList = foreach ($Team in (get-teams | select $TeamDesiredProperyList)){
    $Settings = get-settings -TeamId $Team.Id | select $SettingsDesiredProperyList
    # Assuming the get-settings cmdlet/function has a TeamId parameter, otherwise use: 
    # $Settings = get-settings | where { $_.teamid -eq $ } | select $SettingsDesiredProperyList

    # Now build your PS object to use for the script:
    $Temp = New-Object -TypeName PSObject -Property @{ TeamId = $team.Id }
    foreach ($Property in $TeamDesiredProperyList) {
        $Temp | Add-Member -MemberType NoteProperty -Name $Property -Value $Team.$Property
    foreach ($Property in $SettingsDesiredProperyList) {
        $Temp | Add-Member -MemberType NoteProperty -Name $Property -Value $Settings.$Property

This is coded such that if you would like to add/remove properties from your custom object, you only need to change the $TeamDesiredProperyList and/or $SettingsDesiredProperyList arrays…

That should be straightforward. In second function use get-variable

Function two{ foreach ($team in $TeamsSettings ){ 
$teamid= $team.Id # 
$settings = (get-variable -name "settings$($teamid)").value 


Oh, I tried various things but get-variable. Works great. Thanks Sasa :slight_smile:

Thank you Sam for your help as well. I was using functions so I needed to get variable outside of the function that are dynamically created and Sasa’s solutions worked great for me. Thanks again