想填充以下PSObject,以便IP和主机名之间可以具有一对一或一对多的关系。
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value ""
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value ""
这给了我正确的标题,但是当我尝试添加多个值时,尝试添加重复值会出错:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
这是为什么?
可以相当容易地进行一对多操作:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1", "server2", "server3"
这些都是其中之一的数据集:
1.1.1.1 server1
2.2.2.2 server2
3.3.3.3 server3
4.5.6.7 server4 server5 server6 server7
8.8.8.8 server8
9.9.9.9 server9
1.1.1.1
server1
2.2.2.2
server2
3.3.3.3
server3
4.5.6.7
server4
server5
server6
server7
8.8.8.8
server8
9.9.9.9
server9
答案 0 :(得分:0)
您要创建一个对象列表,例如:
$ArrayList = New-Object System.Collections.ArrayList
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$ArrayList.Add($hostobj)
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
$ArrayList.Add($hostobj)
,以便$ArrayList
包含所有主机对象。并且每个对象都有一个ip_address
和hostnames
的值。
如果这对您没有帮助,请指定您的期望/您打算做什么/使用什么。