1

Im trying to create an array of object with fields depending on certain condition. I have ths code:

$Array  = @(1,2,3,4,5,6,3,4,5,7,9,0,8,9)
$CurrentFolder = Split-Path -parent $MyInvocation.MyCommand.Definition
$Report = @()
foreach($Item in $Array)
{
    $Obj = New-Object PSObject                  

    $Obj | Add-Member -MemberType NoteProperty -Name "Item" -Value $Item            

    switch ($Item)
    {
        1   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_1" -Value "Value_Field_1"}
        2   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_2" -Value "Value_Field_2"}
        3   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_3" -Value "Value_Field_3"}
        4   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_4" -Value "Value_Field_4"}
        5   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_5" -Value "Value_Field_5"}
        6   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_6" -Value "Value_Field_6"}
        7   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_7" -Value "Value_Field_7"}
        8   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_8" -Value "Value_Field_8"}
        9   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_9" -Value "Value_Field_9"}
        0   { $Obj | Add-Member -MemberType NoteProperty -Name "Field_0" -Value "Value_Field_0"}
    }
    $Report = $Report + $Obj
}
$Report | ft -autosize
#$Report | Get-member

When I execute this code I get just two properties. Item and Field_1, I get the property Field_1 because the number 1 is the first item on the array variable.

I would like to get an array with all the properties Field_0 to Field_9 and its values either if they are empty or not.

I tried this using Hash tables, but I got the same result.

1 Answer 1

2

You have to create each property of the object (not in the switch). Then instead of using a switch, you can access the actual property and assign it:

$Array  = @(1,2,3,4,5,6,3,4,5,7,9,0,8,9)
$CurrentFolder = Split-Path -parent $MyInvocation.MyCommand.Definition
$Report = @()
foreach($Item in $Array)
{
    $Obj = New-Object PSObject                  

    $Obj | Add-Member -MemberType NoteProperty -Name "Item" -Value $Item 
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_1" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_2" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_3" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_4" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_5" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_6" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_7" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_8" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_9" -Value $null
    $Obj | Add-Member -MemberType NoteProperty -Name "Field_0" -Value $null

    $obj."Field_$Item" = $Item    

    $Report = $Report + $Obj
}
$Report | ft -autosize

Output:

Item Field_1 Field_2 Field_3 Field_4 Field_5 Field_6 Field_7 Field_8 Field_9
---- ------- ------- ------- ------- ------- ------- ------- ------- -------
   1       1                                                                
   2         2                                                              
   3                 3                                                      
   4                         4                                              
   5                                 5                                      
   6                                         6                              
   3                 3                                                      
   4                         4                                              
   5                                 5                                      
   7                                                 7                      
   9                                                                 9      
   0                                                                        
   8                                                         8              
   9                                                                 9      

Note: If you are using PowerShell > 2, you can also achieve the same using:

$Report = @(1,2,3,4,5,6,3,4,5,7,9,0,8,9) | ForEach-Object {
    $Obj = [PsCustomObject]@{
        Item = $_
        Field_1 = $null
        Field_2 = $null
        Field_3 = $null
        Field_4 = $null
        Field_5 = $null
        Field_6 = $null
        Field_7 = $null
        Field_8 = $null
        Field_9 = $null
        Field_0 = $null
    }          

    $Obj."Field_$_" = $_
    $Obj
}
$Report | ft -autosize
Sign up to request clarification or add additional context in comments.

5 Comments

Consider New-Object psobject -Property @{ Item = $Item; ... } for v2.0 instead of Add-Member (terribly slow)
@Martin, did you notice that the table has no "Field_0" in the output and that only Field_1 and Item are of type Int32 ? Why ?
@LotPings Thats probably because there are to much columns and the last one gets truncated. But the field is there which you can see if you omit the |ft -autosize pipe. To force all items to be an int, you can write [int] before each field.
Thank you very much for you answers , but in this case you know the name of the fields, but, what about if you dont know the name of the fileds?. It will not be possible to create an object with all the properties, basically because i don know all of them.
Just retrieve a distinct list of all values and create that fileds. If you have trouble doing that, ask a new quesiton and we can help you.

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.