The Case of Unknown Custom Object Property
In the last two or three months, I have been busy working on a complete end to end deployment automation solution and been writing lot of PowerShell code. Literally, lot of code. A simple to use deployment automation solution always has complex implementation beneath. I was dealing with several configuration files, dynamically deciding what configuration to fetch and update, and dealing with hundreds of lines of dynamically generated JSON for progress tracking and so on.
While working with one such JSON (shown below), I was seeing a strange error when trying to update a specific property within the JSON.
This is a very minimal and simplified version of JSON that I have in the automation framework. In this JSON, based on the status of deployment, I need to update the status property of each system in the manifest.
Here is what I tried.
Now, that error is pretty strange. To investigate this, I looked at the type of object that was getting returned from the Where() method.
Update (2/11): Prasoon commented on this post and mentioned that the Item() method on this collection to update the status property. Here is how we do it based on his suggestion
What you see below is my investigation before Prasoon commented on this post!
It should ideally be a PS custom object. The where() method is therefore doing something to the custom object. I tried, then, using the index of an object within the systems collection.
This is good. So, I can work around the issue with Where() method by using the index but the only catch here is that I need to dynamically determine the index of a specific object instance during the orchestration. I tried a couple of methods.
I was skeptical about the above method of using
where() again. And, it does fail. The index of the object instance returned using this method is always -1.
In the second method, I resorted to using a simple for loop to gather the index of the node.
The above snippet does not look super optimal to me but works as expected.
I have not figured out any other optimal way of handling this but have you come across something like this? Do you see a better way to handle this?Share on: