gpt4 book ai didi

arrays - 无法索引到数组以将其转换为哈希表

转载 作者:行者123 更新时间:2023-12-03 01:19:42 24 4
gpt4 key购买 nike

我正在尝试将数组转换为哈希表,但我不断收到此错误

无法索引到System.Management.Automation.PSObject类型的对象中。

我到处搜索,找不到其他人遇到相同的问题。

我的代码:

[array]$compArray = $ds3 | select -Property DeviceName, IP_Address

$DeviceHashtable = @{}
$compArray[0][0]
for ($i=0;$i -lt $compArray.length;$i++)
{
$1=[string]$compArray[0][$i];
$2=[string]$compArray[1][$i];
$DeviceHashTable.add("$1", "$2")
}

$ ds3是一个system.data.datatable对象
如果我做$ compArray |显示表我想要的所有数据都在那里。
任何帮助表示赞赏:)

最佳答案

试试这个:

$compArray = $ds3 | select -Property DeviceName, IP_Address
$DeviceHashtable = @{}
$compArray | % { $DeviceHashtable.add( $_.DeviceName, $_.IP_Address )}

关于arrays - 无法索引到数组以将其转换为哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com