gpt4 book ai didi

object - 哈希表中的对象

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

我试图将对象放在哈希表中。我没有收到错误,但是无法访问数据。

$Level1Hashtable = @{}
$Level2Object = New-Object System.Object
$Level2Object | Add-Member -MemberType NoteProperty -Name Name -Value "abc"
$Level2Object | Add-Member -MemberType NoteProperty -Name IpAddress -Value "192.168.1.1"
$Level1Hashtable.Add("Test1",$Level2Object)
$Level2Object = New-Object System.Object
$Level2Object | Add-Member -MemberType NoteProperty -Name Name -Value "123"
$Level2Object | Add-Member -MemberType NoteProperty -Name IpAddress -Value "192.168.1.1"
$Level1Hashtable.Add("Test2",$Level2Object)

$Level1Hashtable.Test1.IpAddress

最佳答案

使用V4对我来说有效。在V2中运行它不起作用,但是如果我从使用System.Object切换到PSObject作为New-Object cmdlet中的对象类型,则可以。

$Level1Hashtable = @{}
$Level2Object = New-Object PSObject
$Level2Object | Add-Member -MemberType NoteProperty -Name Name -Value "abc"
$Level2Object | Add-Member -MemberType NoteProperty -Name IpAddress -Value "192.168.1.1"
$Level1Hashtable.Add("Test1",$Level2Object)
$Level2Object = New-Object PSObject
$Level2Object | Add-Member -MemberType NoteProperty -Name Name -Value "123"
$Level2Object | Add-Member -MemberType NoteProperty -Name IpAddress -Value "192.168.1.1"
$Level1Hashtable.Add("Test2",$Level2Object)

$Level1Hashtable.Test1.IpAddress

关于object - 哈希表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19660757/

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