Powershell 哈希表数组

Powershell Array of hashtables

例如,我有 3 个具有不同键值的哈希表:

$Queue1 = @{
    QueueName = 'MyTestQueue1'
    LockDuration = 'PT1M1'
    AutoDeleteOnIdle = 'P10675199DT2H48M5.4775807S1'
}

$Queue2 = @{
    QueueName = 'MyTestQueue2'
    LockDuration = 'PT1M2'
    AutoDeleteOnIdle = 'P10675199DT2H48M5.4775807S2'
}

$Queue3 = @{
    QueueName = 'MyTestQueue3'
    LockDuration = 'PT1M3'
    AutoDeleteOnIdle = 'P10675199DT2H48M5.4775807S3'
}

是否可以将它们组合在数组中并在 foreach 中使用每个键值?

foreach($hashtable in $Hashtables){
    
    $QueueName = $hashtable.QueueName
    $LockDuration = $hashtable.LockDuration 
    $AutoDeleteOnIdle = $hashtable.AutoDeleteOnIdle
    # Some code here
    }

要声明一个数组,如 here 所示,您可以使用以下语法:

$variable = @(element1, element2, element3, ...)

所以要为您的队列创建一个数组:

$Hashtables = @($Queue1, $Queue2, $Queue3)

您也可以将其分成多行,在该变体中,逗号是可选的:

$Hashtables = @(
    $Queue1
    $Queue2
    $Queue3
)