Home > OS >  Adding to object/array of array objects powershell
Adding to object/array of array objects powershell

Time:01-03

This should be simple but powershell is not giving me a valid output. I am open to other ways to handle this issue. in a high level I need something I can utilize from a parameter of a function if another option is suggested.

here is what I am trying to do:

$PrinterPermissions = 
    @( 
        @('Administrators', 'ManagePrinters','Allow'), 
        @('Power Users', 'ManagePrinters','Allow'), 
        @('Print Operators', 'ManagePrinters','Allow'), 
        @('OHD – Network Support Team', 'ManagePrinters','Allow'),
        @("OHD – PC Support Team", 'ManageDocuments','Allow'),
        @("OHD - Service Desk Users", 'ManageDocuments','Allow')
       
    )

$PrinterPermissions  = @("gprt_PrinterA","ManageDocuments","Allow")

To Access the object I am expecting to to call it like:

$PrinterPermissions[0][0] *= Administrators*
$PrinterPermissions[0][1] *= ManagePrinters*
$PrinterPermissions[0][2] *= Allow*

However when I call the newly added object to the array I get the first 6 objects work correctly but the newly added object functions like:

$PrinterPermissions[6]    = gprt_PrinterA
$PrinterPermissions[6][0] = g
$PrinterPermissions[6][1] = p
$PrinterPermissions[6][2] = r
$PrinterPermissions[7]    = ManageDocuments
$PrinterPermissions[8]    = Allow

What I want is for the newly added item to function in the array as the first 6 items. So I can dynamically add to the array. What am I doing wrong.

CodePudding user response:

I agree with mklement0 you would do better to use a Generic List for this job, as it is designed to add/remove items unlike a 'normal' array, which has to be reconstructed in memory as a whole on every addition.

Furthermore, Why bother doing this as array of arrays, when you can do this a lot cleaner (IMO) using objects (as the title of your question suggests)

# create a new List of objects
$PrinterPermissions = [System.Collections.Generic.List[object]]::new()

# set up the initial 6 items in the list
$PrinterPermissions.AddRange(([PsCustomObject]@{ Group = 'Administrators'; Permission = 'ManagePrinters'; AccessType = 'Allow' },
                              [PsCustomObject]@{ Group = 'Power Users'; Permission = 'ManagePrinters'; AccessType = 'Allow' },
                              [PsCustomObject]@{ Group = 'Print Operators'; Permission = 'ManagePrinters'; AccessType = 'Allow' },
                              [PsCustomObject]@{ Group = 'OHD – Network Support Team'; Permission = 'ManagePrinters'; AccessType = 'Allow' },
                              [PsCustomObject]@{ Group = 'OHD – PC Support Team'; Permission = 'ManagePrinters'; AccessType = 'Allow' },
                              [PsCustomObject]@{ Group = 'OHD - Service Desk Users'; Permission = 'ManagePrinters'; AccessType = 'Allow' }))

# add a new item to the list
$PrinterPermissions.Add([PsCustomObject]@{ Group = 'gprt_PrinterA'; Permission = 'ManagePrinters'; AccessType = 'Allow' })

# show on screen
$PrinterPermissions

Result

Group                      Permission     AccessType
-----                      ----------     ----------
Administrators             ManagePrinters Allow     
Power Users                ManagePrinters Allow     
Print Operators            ManagePrinters Allow     
OHD – Network Support Team ManagePrinters Allow     
OHD – PC Support Team      ManagePrinters Allow     
OHD - Service Desk Users   ManagePrinters Allow     
gprt_PrinterA              ManagePrinters Allow

Retrieve the 7th item:

$PrinterPermissions[6]

Result

Group         Permission     AccessType
-----         ----------     ----------
gprt_PrinterA ManagePrinters Allow  

Get individual properties from an item in the list

$PrinterPermissions[5].Group        # --> OHD - Service Desk Users
$PrinterPermissions[5].Permission   # --> Allow

Save the list as CSV file for later use (perhaps in Excel)

$PrinterPermissions | Export-Csv -Path 'X:\PrinterPermissions.csv' -UseCulture -NoTypeInformation

etc.

CodePudding user response:

Note:

  • This answer addresses the question as asked.

  • For an ultimately better solution that uses custom objects rather than nested arrays, see Theo's helpful answer.

In order to add an RHS array as a single element, you must wrap it in a transient single-element wrapper array using the unary form of the array-construction operator, ,.

$PrinterPermissions  = , @("gprt_PrinterA","ManageDocuments","Allow")

Note that adding to an array technically creates a new array every time, which is inefficient. For small arrays, that probably won't matter, but for iteratively adding many elements you should consider switching to an efficiently extensible list type, such as [System.Collections.Generic.List[string]]

  • Related