Syntax Error
Syntax Error

Reputation: 1640

How to return string value from an array, based on another variable?

I have the following code and would like to return the string value for aaa, depending on what is in the ADProps.physicalDeliveryOfficeName supplied to the GetOfficeLocation function.

The switch seems to do what it should but it won't return the string value, perhaps I am referencing it incorrectly when outputting $a["aaa"]?

$global:newcastle = @{
"Value 1 newcastle" = @{
    "aaa" = "newcastle string";
    }
}

$global:london = @{
"Value 1 london" = @{
    "aaa" = "london string";
    }
}

$global:heathrow = @{
"Value 1 heathrow" = @{
    "aaa" = "heathrow string";
    }
}

$ADProps=@{
    'physicalDeliveryOfficeName'= "heathrow airport";
}

function GetOfficeLocation ($office) {
    switch ( $office )
    {
    "newcastle" {$location = "newcastle"; break}
    "london city" {$location = "london"; break}
    "heathrow airport" {$location = "heathrow"; break}
    }
    return $location
}

$a = GetOfficeLocation($ADProps.physicalDeliveryOfficeName)
$a["aaa"]

Result is that nothing gets output to the console.

Desired result in this example would be for this to be displayed: heathrow string

Effectively I am trying to determine which @global variable to choose and then access its members from then on.

edit

How do I return the value heathrow string, based on passing heathrow airport as a parameter into the GetOfficeLocation function? I would also like to be able to return newcastle string or london string by changing the input accordingly.

Upvotes: 1

Views: 54

Answers (2)

Kirill Pashkov
Kirill Pashkov

Reputation: 3236

You could reach this using hashtable. Like so:

$HashTable = @{
    'newcastle' = 'newcastle';
    'london city' = 'london';
    'heathrow airport' = 'heathrow';
}

$ADProps=@{
    'physicalDeliveryOfficeName'= "heathrow airport";
}

Calling key 'heathrow airport' will return its corresponding value heathrow

$HashTable[$ADProps.physicalDeliveryOfficeName]
heathrow

Upvotes: 2

4c74356b41
4c74356b41

Reputation: 72176

i think what you are trying to do is something like this:

heathrow = @{
    aaa = "heathrow string"
}
$a = GetOfficeLocation($ADProps.physicalDeliveryOfficeName)
(Get-Variable -Name $a).value.aaa

but i dont know, code is completely not clear

Upvotes: 1

Related Questions