我有一个从API中提取的JSON对象,我需要从中提取IP地址。
数据存储在一个变量中,如下所示:
echo $IPMIAddr
count next previous results
----- ---- -------- -------
1 {@{id=17247; family=4; address=10.2.63.142/24; vrf=; tenant=; status=; role=; interface=; description=; ...
我可以使用点来解析数据以达到所需的效果
echo $IPMIAddr.results
id : 17247
family : 4
address : 10.2.63.142/24
vrf :
tenant :
status : @{value=1; label=Active}
role :
interface : @{id=50554;
url=http://netbox/api/dcim/interfaces/50554/; device=;
virtual_machine=; name=IPMI}
description :
nat_inside :
nat_outside :
custom_fields :
created : 2018-05-03
last_updated : 2018-05-03T08:08:36.856098Z
因此下一个逻辑步骤将是:
echo $IPMIAddr.results.address
但是当我这样做时,我得到了错误:
OverloadDefinitions
-------------------
System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )
因为.address是PowerShell中的预定义函数。
我该如何克服?
编辑:
结果:
Get-Member -InputObject $IPMIAddr
是
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
count NoteProperty int count=1
next NoteProperty object next=null
previous NoteProperty object previous=null
results NoteProperty Object[] results=System.Object[]
和
Get-Member -InputObject $IPMIAddr.results
TypeName: System.Object[]
Name MemberType Definition
---- ---------- ----------
Count AliasProperty Count = Length
Add Method int IList.Add(System.Object value)
Address Method System.Object&, mscorlib,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b...
Clear Method void IList.Clear()
Clone Method System.Object Clone(), System.Object
ICloneable.Clone()
CompareTo Method int
IStructuralComparable.CompareTo(System.Object other, System.Collections....
Contains Method bool IList.Contains(System.Object value)
CopyTo Method void CopyTo(array array, int index),void CopyTo(array array, long index), v...
Equals Method bool Equals(System.Object obj), bool
IStructuralEquatable.Equals(System.Obje...
Get Method System.Object Get(int )
GetEnumerator Method System.Collections.IEnumerator
GetEnumerator(), System.Collections.IEnumerat...
GetHashCode Method int GetHashCode(), int
IStructuralEquatable.GetHashCode(System.Collections.I...
GetLength Method int GetLength(int dimension)
GetLongLength Method long GetLongLength(int dimension)
GetLowerBound Method int GetLowerBound(int dimension)
GetType Method type GetType()
GetUpperBound Method int GetUpperBound(int dimension)
GetValue Method System.Object GetValue(Params int[]
indices), System.Object GetValue(int ind...
IndexOf Method int IList.IndexOf(System.Object value)
Initialize Method void Initialize()
Insert Method void IList.Insert(int index, System.Object value)
Remove Method void IList.Remove(System.Object value)
RemoveAt Method void IList.RemoveAt(int index)
Set Method void Set(int , System.Object )
SetValue Method void SetValue(System.Object value, int index), void SetValue(System.Object v...
ToString Method string ToString()
Item ParameterizedProperty System.Object IList.Item(int index) {get;set;}
IsFixedSize Property bool IsFixedSize {get;}
IsReadOnly Property bool IsReadOnly {get;}
IsSynchronized Property bool IsSynchronized {get;}
Length Property int Length {get;}
LongLength Property long LongLength {get;}
Rank Property int Rank {get;}
SyncRoot Property System.Object SyncRoot {get;}
答案 0 :(得分:1)
看起来您的.results
成员实际上是一个数组,其中包含一个名为Address
的成员。您需要索引到数组中以访问适当的成员:
$IPMIaddr.results[0].address
ForEach-Object
起作用的原因是由于枚举:
$IPMIaddr.results | Foreach-Object -MemberName address
这会将address
成员拉离所有results
对象。