1

Anyone good with hash tables ale to help out on this one. I have multiple global groups where I am gather each member of the groups. I am trying to add them to a hash table, each line contains all the information about the user account being returned where the $group is the key.

When I use $vdiArr.add I get the error Cannot find an overload for "add" and the argument count: "9". When I remove the hashed out command it works fine but the data in the array seems to be all over the place. I am trying to achieve each user's information on one line within the array. e.g vdiArr[0]={theirGroup,Name, GivenName, LastName and so on....

clear-host
$count = $null
$date = Get-Date -format "MM-dd-yyyy_hh-mm-ss"
$vdiGrps = Get-Content "D:\SourceFiles$\Scripts\Active Directory\ADGroups.txt"

$vdiArr=[System.Collections.ArrayList]@{}
$vdiArr=@{}

    foreach($group in $vdiGrps)
    {
        $ADgroup=(get-adgroup $group -properties members).members | get-adobject | where {$_.objectclass -eq "User"}| get-aduser -properties * | select name, GivenName, Surname, UserPrincipalName, SamAccountName, Enabled, DistinguishedName
        $ADgroup

        $vdiArr.add($group,$ADgroup.name,$ADgroup.GivenName,$ADgroup.Surname,$ADgroup.UserPrincipalName,$ADgroup.SamAccountName,$ADgroup.SamAccountName,$ADgroup.Enabled,$ADgroup.DistinguishedName)

        #$vdiArr.group = $group
        #$vdiArr.Name = $ADgroup.name
        #$vdiArr.GivenName = $ADgroup.GivenName
        #$vdiArr.Surname = $ADgroup.Surname
        #$vdiArr.UserPrincipalName = $ADgroup.UserPrincipalName
        #$vdiArr.SamAccountName = $ADgroup.SamAccountName
        #$vdiArr.Enabled = $ADgroup.Enabled
        #$vdiArr.DistinguishedName = $ADgroup.DistinguishedName


    }


$arrOutput += New-Object -TypeName psobject -Property $vdiArr
Out-File -InputObject $arrOutput -FilePath "$home\Desktop\test.csv"

1 Answer 1

1

Try the following:

$vdiGrps = Get-Content "D:\SourceFiles$\Scripts\Active Directory\ADGroups.txt"

# Create empty array list.
$vdiArr=[System.Collections.ArrayList] @()

foreach($group in $vdiGrps)
{

    # Get users in group.
    $ADgroup=(get-adgroup $group -properties members).members | get-adobject | where {$_.objectclass -eq "User"}| get-aduser -properties * | select name, GivenName, Surname, UserPrincipalName, SamAccountName, Enabled, DistinguishedName

    # Loop over all users in group and add them to the output array.
    foreach($user in $ADgroup) {
      $null = $vdiArr.Add([pscustomobject] @{
        Group = $group
        Name = $user.name
        GivenName = $user.GivenName
        Surname = $user.Surname
        UserPrincipalName = $user.UserPrincipalName
        SamAccountName = $user.SamAccountName
        Enabled = $user.Enabled
        DistinguishedName = $user.DistinguishedName
      })
    }

}

Out-File -InputObject $vdiArr -FilePath "$HOME\Desktop\test.csv"
  • $null = ... suppresses the (undesired) output from the .Add() call.

  • The above approach demonstrates how to construct a custom object from a hashtable, but note that if you added a Group property as a calculated property to your Select-Object call, you could add the resulting objects directly to the output array.


As for what you've tried:

$vdiArr=[System.Collections.ArrayList]@{} is probably not what you intended, because casting the empty hashtable literal - @{} - to [System.Collections.ArrayList] results in an empty array list - the same as [System.Collections.ArrayList] @() (casting from an empty array literal) or New-Object System.Collections.ArrayList

That said, your very next statement - $vdiArr=@{} - completely overrides your previous statement and assigns an empty hashtable to $vdiArr, making the type of $vdiArr [hashtable] ([System.Collections.Hashtable]).

Your .Add() call then fails, because a hashtable's .Add() method only accepts 2 arguments - the key and the name of the new entry - not the 9 you're giving it.

That said, even if $vdiArr were a [System.Collections.ArrayList] instance, as you probably intended, the .Add() call wouldn't work, because [System.Collections.ArrayList]'s .Add() method only accepts a single argument - the new element to add.

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.