Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with using properties in an array and custom calculated property

So I have the following code that ingests AD users on a domain controller. The following throws an error:

# User Props to select
$user_props = @(
        'Name',
        'DistinguishedName',
        'SamAccountName',
        'Enabled',
        'SID'
        )

# Get AD groups an AD user is a member of
$user_groups = @{ label = 'GroupMemberships'; expression = { (Get-ADPrincipalGroupMembership -Identity $_.DistinguishedName).Name } }

# Get AD Users
$users = Get-ADUser -Filter * -Property $user_props | Select-Object $user_props, $user_groups -ErrorAction Stop -ErrorVariable _error

However, if I were to change $users to the following:

$users = Get-ADUser -Filter * -Property $user_props | Select-Object Name, DistinguishedName, SamAccountName, Enabled, SID, $user_groups -ErrorAction Stop -ErrorVariable _error

I no longer get this error. Is there a way I can define $user_props such that I don’t need to type out each property and still use my custom calculated property $user_groups?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I believe the issue has to do with mixing an array ($user_props) with a hashtable ($user_groups) but I’m unsure how to best write this. Thank you for the help!

>Solution :

The easiest way to "concatenate" two variables into one flat array is to use the @(...) array subexpression operator:

... |Select-Object @($user_props;$user_groups) ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading