Adrien S.
Adrien S.

Reputation: 3

Accessing hash tables values in the same hash table in powershell

I'm storing a lot of variables that depend on other variables, that themselves depend on other variables, like so :

$soft_version = "5.8.2"
$soft_filename = "somesoft-${soft_version}.exe"
$soft_path = "/some/path/$soft_filename"
$soft_call_args = "/S /forcerestart"

And I'd like to store this in a hash table so that I can access the values more easily : so I tried the following

$soft = @{
version="5.8.2";
filename="somesoft-${soft.version}.exe"
path="/some/path/$soft.filename"
call_args = "/S /forcerestart"
}

So that I can access the values like this :

$soft.version
$soft.filename
$soft.path
$soft.call_args

But it doesn't seem to work, corresponding keys remain empty :

$soft

Name                           Value                                                                                               
----                           -----                                                                                               
filename                       somesoft-.exe
path                           /some/path/
call_args                      /S /forcerestart
version                        5.8.2

Is there a way I could access values of an hash table inside the same hash table ? Thanks :-)

Upvotes: 0

Views: 501

Answers (2)

Jaqueline Vanek
Jaqueline Vanek

Reputation: 1133

you can also use the new v5 classes or custom objects

PowerShell: Creating Custom Objects

Upvotes: 0

Martin
Martin

Reputation: 1963

Try to wrap in brackets, this works for me (PSVersion 5):

$soft = @{
version="5.8.2";
filename="somesoft-$($soft.version).exe"
path="/some/path/$($soft.filename)"
call_args = "/S /forcerestart"
}

Result

$soft

Name                           Value   
----                           -----
filename                       somesoft-5.8.2.exe
path                           /some/path/somesoft-5.8.2.exe
call_args                      /S /forcerestart
version                        5.8.2

Upvotes: 1

Related Questions