0

Hi I have n sum of objects for each Node like this:

NodeName                              : 11111
System.AreaId                         : 2375
System.AreaPath                       : Project
System.TeamProject                    : Project
System.NodeName                       : Project
System.AreaLevel1                     : Project

Every node can have different objects in it. How can I split them to an arrays/strings without specifying the object name so I can create foreach separate object loop?

5
  • Can you edit your question displaying what would be the expected output ? Commented May 23, 2021 at 19:50
  • 3
    You can invoke .psobject.Properties on any object in PowerShell to get a collection of its public properties. Commented May 23, 2021 at 19:50
  • Thanks @mklement0 I didn't know that. This is what I wanted. Can you recommend me PowerShell readings for useful tricks like this one? Commented May 23, 2021 at 19:53
  • @Baequiraheal, I compiled a list of learning resource in this answer a while ago. Fundamentally, learning how to use PowerShell's help system efficiently and studying the conceptual about_* topics helps, though they don't tell the full story. There's also good information in the answers here on SO. Commented May 23, 2021 at 21:06
  • If this comes from a Json file consider to use the ConvertFrom-Json -AsHashTable parameter as it gives you an easier access to the keys. Commented May 24, 2021 at 7:43

1 Answer 1

2

mklement0 beat me to what I was going to post. Since I have the code drafted already I will post it.

Like mklement0 said in comments, you can access object properties through use of .psobject.Properties. Below in the code I am using a switch statement to check if an object contains a specific property.

$objs = @(
[pscustomobject]@{
    AreaId      = 2375
    AreaPath    = ''
    TeamProject = 'Project2'
    NodeName    = ''
    AreaLevel1  = ''
},
[pscustomobject]@{
    AreaId      = 342
    AreaPath    = ''
    TeamProject = 'Project2'
    Color       = 'Red'
}

)

switch ($objs) {
    { $_.psobject.properties.name -contains 'Color' } {
        'Object contains Color property'
    }
    { $_.psobject.properties.name -contains 'NodeName' } {
        'Object contains NodeName property'
    }
    Default {}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.