如何根据另一个变量 return 数组中的字符串值?
How to return string value from an array, based on another variable?
我有以下代码,并希望 return aaa
的字符串值,具体取决于提供给 GetOfficeLocation
函数的 ADProps.physicalDeliveryOfficeName
中的内容。
开关似乎做了它应该做的,但它不会 return 字符串值,也许我在输出时引用不正确 $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"]
结果是控制台没有任何输出。
此示例中的预期结果将显示为:heathrow string
实际上,我正在尝试确定选择哪个@global 变量,然后从那时起访问它的成员。
编辑
如何根据将 heathrow airport
作为参数传递给 GetOfficeLocation
函数来 return 值 heathrow string
?我还希望能够通过相应地更改输入来 return newcastle string
或 london string
。
我想你想做的是这样的:
heathrow = @{
aaa = "heathrow string"
}
$a = GetOfficeLocation($ADProps.physicalDeliveryOfficeName)
(Get-Variable -Name $a).value.aaa
不知道,代码完全看不懂
您可以使用哈希表来达到这个目的。像这样:
$HashTable = @{
'newcastle' = 'newcastle';
'london city' = 'london';
'heathrow airport' = 'heathrow';
}
$ADProps=@{
'physicalDeliveryOfficeName'= "heathrow airport";
}
调用键'heathrow airport'
将return其对应的值heathrow
$HashTable[$ADProps.physicalDeliveryOfficeName]
heathrow
我有以下代码,并希望 return aaa
的字符串值,具体取决于提供给 GetOfficeLocation
函数的 ADProps.physicalDeliveryOfficeName
中的内容。
开关似乎做了它应该做的,但它不会 return 字符串值,也许我在输出时引用不正确 $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"]
结果是控制台没有任何输出。
此示例中的预期结果将显示为:heathrow string
实际上,我正在尝试确定选择哪个@global 变量,然后从那时起访问它的成员。
编辑
如何根据将 heathrow airport
作为参数传递给 GetOfficeLocation
函数来 return 值 heathrow string
?我还希望能够通过相应地更改输入来 return newcastle string
或 london string
。
我想你想做的是这样的:
heathrow = @{
aaa = "heathrow string"
}
$a = GetOfficeLocation($ADProps.physicalDeliveryOfficeName)
(Get-Variable -Name $a).value.aaa
不知道,代码完全看不懂
您可以使用哈希表来达到这个目的。像这样:
$HashTable = @{
'newcastle' = 'newcastle';
'london city' = 'london';
'heathrow airport' = 'heathrow';
}
$ADProps=@{
'physicalDeliveryOfficeName'= "heathrow airport";
}
调用键'heathrow airport'
将return其对应的值heathrow
$HashTable[$ADProps.physicalDeliveryOfficeName]
heathrow