![](/img/trans.png)
[英]How to create a custom hashtable with arrays of objects - powershell / cmd
[英]How to create arrays of hashtable in powershell?
我有一個具有以下結構的哈希表,
$podhashtable = @{2 = '1','2' ; 3 = '3','4','5' ; 1 = '6'}
這是我如何實現上述哈希表的代碼,
$podnumbers = @(2,3,1)
$podInfo = @()
$podhashtable = [ordered]@{}
$buff = 0
foreach ($pd in $podnumbers) {
#$podInfo = @()
for($i=0;$i -lt $pd;$i = $i+1) {
$pod = Read-Host -Prompt "Assign the pod numbers for",$esxarray[$buff]
$podinfo += $pod
}
$podInfoOb = New-Object PSObject –Property $podsets
$podsets = @{$pd = $podInfo}
$podhashtable += $podsets
$buff = $buff + 1
}
Write-Output $podhashtable
我正在嘗試創建如下所示的哈希表數組,
$podhashtable = @{2 = '1','2'}, @{3 = '3','4','5'}, @{1 = '6'}
獲得上述哈希表數組的最佳方法是什么?
這個問題顯然是指:如何將具有不同值的相同鍵添加到 powershell 中的哈希表?
我猜你想這樣做:
$buff = 0
$podnumbers = @(2,3,1,2)
$podhashtable = foreach ($pd in $podnumbers) {
$podInfo = @{}
for($i=0; $i -lt $pd; $i = $i+1) {
$pod = Read-Host -Prompt "Assign the pod numbers for" # , $esxarray[$buff]
$podinfo[$i] = $pod
# $podInfoOb = New-Object PSObject –Property $podsets
$podInfo
$buff = $buff + 1
}
}
Write-Output $podhashtable
但我不能確定,因為您沒有提供MCVE (請下次提供;例如:定義$esxarray
或將其省略)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.