Changing an array column name

12,359

Solution 1

Agreed that this is over-engineered, however you can modify the object in place.

Assuming your object array is $permissions, you can iterate over it, add a member of $result with value from property Plant manager and then remove the Plant manager property:

$permissions | % { 
    $_ | add-member "$result" -NotePropertyValue $_."Plant Manager"
    $_.psobject.properties.remove("Plant Manager")
}

Solution 2

One option is to use Add-Member to add a new NoteProperty with the desired name, and copy the value from the existing one.

That being said, it seems like you're over-engineering this quite a bit.

How about a calculated property instead:

$Permissions = $Permissions |Select-Object @{Name="Belgium Awesome Plant Manager"; Expression = {$_.'Plant Manager'} },'Sales man','Warehouseman'

Solution 3

The ReadOnly attribute of the object property is going to be dictated by the object type. One way around this is to run the object thorugh Select *. This will change the object type to either a PSSelectedObject or PSCustomObject, depending on your PS version, and the property should lose the ReadOnly attribute in the process:

$Permissions | Get-Member | ? MemberType -EQ NoteProperty | Select * |% {
Share:
12,359
DarkLite1
Author by

DarkLite1

Master Bruce... why do we fall? So we can learn to stand up again.

Updated on June 14, 2022

Comments

  • DarkLite1
    DarkLite1 almost 2 years

    I have an array that contains NotePoperties that needs to be modified. When reading the array I need to do some code to figure out what the correct title for the array column should be.

    This code is ready and works fine:

    $Permissions | Get-Member | ? MemberType -EQ NoteProperty | % {
        $Column = $Permissions.($_.Name)
        $GroupResult = Switch ($Column[1]) {
            'GROUP' {$Settings[0].Group  + ' '}
            ''      {break}
            Default {$group + ' '}
        }
        $SiteResult = Switch ($Column[0]) {
            'Site'  {$Settings[0].Code + ' '}
            ''      {break}
            Default {$site + ' '}
        }
        $Result = ($GroupResult + $SiteResult + $_.Name).Trim()
    }
    

    What I'm now trying to do is update/modify the existing title with the generated $Result string from above:

    $_.Name = $Result
    

    When doing this, PowerShell throws the error 'Name' is a ReadOnly property..

    What is the best way of changing the title of the array? I also need to remove line 2 and line 3, but I can do that afterwards.

    Example of the $Permissions array where I'm trying to updated Plant manager to Belgium Awesome Plant manager:

    Plant manager | Sales man | Warehouseman
    -------------- ----------  -------------
    Site          |           |
    Group         | Group     |
    Stuff         | Stuff     | Stuff
    

    Thank you for your help.

  • DarkLite1
    DarkLite1 about 9 years
    Thank you mjolinor, I tried your suggestion but it seems that it doesn't modify the array. When trying $_.Name = $Result I can definitely see that it accepts the new value but after that it's not saved. When saving the whole loop in a test variable it's empty to.
  • DarkLite1
    DarkLite1 about 9 years
    Thank you Mathias, but how do you use your suggestion if you don't know how long the array will be upfront? And you need to calculate the Name= part for every header that's of the type NoteProperty? Plant Manager is just one example, but there are plenty more and some are with Belgium and others aren't. That's why I need the Switch based on the header title.
  • mjolinor
    mjolinor about 9 years
    That's probably a consequence of doing it in the pipeline with $_.
  • DarkLite1
    DarkLite1 about 9 years
    Thank you arco444, this was exactly what I was looking for. Regarding over-engineering, the problem is that I can't simplify the process to generate the property name of the object as it's based on lots of variables. So almost every column needs to be calculated an recreated. Thx again for this great solution :)