代码之家  ›  专栏  ›  技术社区  ›  Syntax Error ine

如何基于另一个变量从数组返回字符串值?

  •  1
  • Syntax Error ine  · 技术社区  · 6 年前

    我有以下代码,并希望返回 aaa ,取决于 ADProps.physicalDeliveryOfficeName 提供给 GetOfficeLocation 功能。

    $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公司 功能?我也希望能回来 newcastle string london string 通过相应地改变输入。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Kirill Pashkov    6 年前

    您可以使用hashtable访问这个。像这样:

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

    呼叫键 'heathrow airport' heathrow

    $HashTable[$ADProps.physicalDeliveryOfficeName]
    heathrow
    
        2
  •  1
  •   4c74356b41    6 年前

    我想你想做的是这样的:

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

    但我不知道,代码是完全不清楚