简体   繁体   中英

PowerShell objects clash with built in functions?

I have a JSON object that I have pulled from an API, and I need to pull an IP address from it.

the data is stored in a variable looks like this :

echo $IPMIAddr

count next previous results                                                                                                    
----- ---- -------- -------                                                                                                    
    1               {@{id=17247; family=4; address=10.2.63.142/24; vrf=; tenant=; status=; role=; interface=; description=; ...

I can parse through the data using dots to get to what I want like this

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

So the next logical step would be:

echo $IPMIAddr.results.address

but when I do this i get the error :

OverloadDefinitions                                                                                        
-------------------                                                                                        
System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )

Because .address is a predefined function within PowerShell.

How do I get past this?

edit :

the result of:

Get-Member -InputObject $IPMIAddr 

is

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[]

and

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;} 

It looks like your .results member is actually an array, which contains a member named Address . You need to index into your array to access the proper member:

$IPMIaddr.results[0].address

The reason ForEach-Object works is due to enumeration:

$IPMIaddr.results | Foreach-Object -MemberName address

This pulls the address member off all the results objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM