0

I'm trying to fetch a value from a hashtable like the one below.

$Hashtable1AuthTestID = @{ 
    "BID_XPI" = "(id 2)";
    "MBID_XPI" = "(id 3)";
    "T_XPI" = "(id 4)";
    "ST_XPI" = "(id 5)";
    "SI_XPI" = "(id 6)";
    "T_SAML" = "(id 7)";
    "ST_SAML" = "(id 8)";
    "SI_SAML" = "(id 9)";
    "BID_SAML" = "(id 10)";
    "MBID_SAML" = "(id 11)";
}

It's working fine if I use $Hashtable1AuthTestID.BID_XPI but since this will be a generic script for several different type of data (and environments) I would like to include several variable when I call the hashtable such as the one below.

# Without variables (Example): $Hashtable1AuthTestID.BID_XPI 
# With variables (Example): $<Hashtable><Type><Environment>.<Method>

$hashtable = "Hashtable1"
$type = "Auth"
$environment = "Test"
$method = "BID_XPI"
# ID is the example is a string.

$'$hashtable1'$environment"ID".$method
$$hashtable1$environment+"ID".$method

I've tested several different approaches but can't get it working. I do get the correct syntax (if I print the values from the variables) such as $Hashtable1AuthTestID.BID_XPI but I don't get the actual value from the hashtable ((id 2)).

2
  • You have duplicated S_XPI key in your example hashtable, might want to correct that. Commented Jun 17, 2019 at 7:49
  • The $environment = "Test" is missing ID. If you build the string with those variables you get $Hashtable1AuthTest.BID_XPI not $Hashtable1AuthTestID.BID_XPI Commented Jun 17, 2019 at 8:04

3 Answers 3

1

Referencing individually named variables by using a name from another variable -although possible- is a misguided approach. Don't do it. The canonical way of dealing with situations like this is to use either an array, if you want to access the data structure or object by index:

$hashtables = @()
$hashtables += @{
    "BID_XPI"  = "(id 2)"
    "MBID_XPI" = "(id 3)"
    ...
}

$hashtables[0].MBID_XPI

or a hashtable, if you want to access the data structure or object by name:

$hashtables = @{}
$hashtables['Hashtable1AuthTestID'] = @{
    "BID_XPI"  = "(id 2)"
    "MBID_XPI" = "(id 3)"
    ...
}

$hashtable   = 'Hashtable1'
$type        = 'Auth'
$environment = 'Test'
$method      = 'BID_XPI'

$name = "${hashtable}${type}${environment}ID"

$hashtables.$name.$method

For the sake of completeness, here is how you would get a variable by using a name from another variable, but again, this is NOT RECOMMENDED.

$Hashtable1AuthTestID = @{
    "BID_XPI"  = "(id 2)"
    "MBID_XPI" = "(id 3)"
    ...
}

$hashtable   = 'Hashtable1'
$type        = 'Auth'
$environment = 'Test'
$method      = 'BID_XPI'

$name = "${hashtable}${type}${environment}ID"

(Get-Variable -Name $name -ValueOnly).$method
Sign up to request clarification or add additional context in comments.

Comments

0

You could use Get-Variable:

$hashtable = "Hashtable1"
$type = "Auth"
$environment = "Test"
$method = "BID_XPI"

(Get-Variable -Name "$($hashtable)$($type)$($environment)ID".).Value.$method

Comments

0

If you are able to print it you can invoke it:

$string = '${0}{1}{2}ID.{3}' -f $hashtable,$type,$environment,$method
Invoke-Expression -Command $string

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.