Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find property on object that exists

I have an array of objects and am trying to manipulate it and am getting an error that the property RptFile does not exist. I have checked the spelling and everything and am baffled as to what is going on.

The code giving the error:

$AllContents | Where-Object {$_.RptFile -eq 'CB-Officer Trial New'} 

AllContents | Get-Member returns:


TypeName: Selected.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()                                     
RptFile      NoteProperty System.String RptFile=ABL - Branch5206 Daily OD Report
TotalSeconds NoteProperty System.String TotalSeconds=25   

So the property does exist. Any idea what is going on? If I just type $AllContents, I get a list with the property also.

like image 863
user1612851 Avatar asked Jun 11 '13 14:06

user1612851


People also ask

How do you check if a property of an object exists?

The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.

What happens if we try to access a property of an object that does not exist?

If the property does not exist on the object, it will return the string undefined.

How do you check if an object contains a property in Java?

JavaScript provides you with three common ways to check if a property exists in an object: Use the hasOwnProperty() method. Use the in operator. Compare property with undefined .

How do you find out if a property is undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.


2 Answers

What is your value for Set-StrictMode can you test to remove strict mode before your code?

Set-StrictMode -Off

What are the results for :

Get-Member -InputObject $AllContents

Get-Member -InputObject $AllContents[0].RptFile
like image 96
JPBlanc Avatar answered Sep 22 '22 14:09

JPBlanc


Try first

$AllContents[0].RptFile = '<value>'

if not something like this should help:

[Your.Interface.Implemented.Explicitly].GetProperty("RptFile").SetValue($AllContents[0], '<value>',$null)
like image 32
Roman Pokrovskij Avatar answered Sep 19 '22 14:09

Roman Pokrovskij