ForEach Creating Objects and Appending to ObjArray

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of kvprasoon kvprasoon 1 year, 7 months ago.

  • Author
    Posts
  • #28095
    Profile photo of Graham Pinkston
    Graham Pinkston
    Participant

    I'm sure I'm just having a brain fart or something, but I can't for the life of me get this simple script to work:

    $StoreLocation = 'OU=bla,OU=bla,OU=bla,DC=bla,DC=bla,DC=Com'
    
    Write-Verbose -Message ":::::::::::::: Starting Script  ::::::::::::::"
    
        $objrow = New-Object PSObject
        $objrow | add-member -membertype NoteProperty -name "MemberName" -value " "
        $objrow | add-member -membertype NoteProperty -name "MemberEmail" -value " "
        $objrow | add-member -membertype NoteProperty -name "MemberGUID" -value " "
        $objrow | add-member -membertype NoteProperty -name "GroupName" -value " "
        $objrow | add-member -membertype NoteProperty -name "GroupEmail" -value " "
       $AllMembers = @()
        $Groups = Get-ADGroup -Properties Mail -Filter * -SearchBase $StoreLocation
    
        ForEach ($Group in $Groups) {
            $Members = Get-ADGroupMember -Identity $Group
            ForEach ($Member In $Members) {
                $User = Get-ADUser -Identity $Member
                $UserName = $Member.SamAccountName
                $objrow.MemberName = $Member.SamAccountName
                $objrow.MemberEmail = $User.UserPrincipalName
                $objrow.MemberGUID = $Member.objectGUID
                $objrow.GroupName = $Group.SamAccountName
                $objrow.GroupEmail = $Group.Mail
                Write-Verbose -Message "Adding member $UserName to objarray..."
                $objrow += $AllMembers
            }
        }
    

    What I end up with is a 1000 objects with all the same info. Last I checked... I should be appending a new object to an array of objects ... so why would it overwrite all the objects currently in the array ?

    Thanks for any help.

  • #28096
    Profile photo of kvprasoon
    kvprasoon
    Participant

    Hi Graham,
    Replacing PScustomObject with a Hashtable here will make your script Little more stable.

    And Some more,

            # A Group Object Contains lot of Vlaues, Here You need 'Name'
            $Members = Get-ADGroupMember -Identity $Group.name
            # And a Group may contain another group as Members,If you are looking for
            # Only User, Use a where-object to Filter by 'objectclass -eq "User"'
            $Members = Get-ADGroupMember -Identity $Group.name |Where-Object       
            {$_.objectclass -eq "User"}
            # Here We don't need a PSObject,We Can Directly Put it intio an Array From 
            # Hashtable, 
            #$objrow += $AllMembers 
            # And Make sure Your SearchBase Value is a Distinguished Name 
    
    
    
    Write-Verbose -Message ":::::::::::::: Starting Script  ::::::::::::::"
    
       $AllMembers = @()
        $Groups = Get-ADGroup -Properties Mail -Filter * -SearchBase $StoreLocation
    
        ForEach ($Group in $Groups) { 
            $Members = Get-ADGroupMember -Identity $Group.name |?{$_.objectclass -eq "User"}
            ForEach ($Member In $Members) {
                $User = Get-ADUser -Identity $Member
                $UserName = $Member.SamAccountName
                $allmembers+=new-object psobject -Property @{
                MemberName = $Member.SamAccountName;
                MemberEmail = $User.UserPrincipalName;
                MemberGUID = $Member.objectGUID;
                GroupName = $Group.SamAccountName;
                GroupEmail = $Group.Mail;
                }
                Write-Verbose -Message "Adding member $UserName to objarray..."
                          
            }
        }
    

    Hope This Helps You.
    Regards.,
    kvprasoon

  • #28103
    Profile photo of Zuldan
    Zuldan
    Participant

    Hi Graham, brain farts are the norm 🙂

    You're adding a an array to a object. You need to add the object to an array.

    This line

    $objrow += $AllMembers

    Should be

    $AllMembers += $objrow

    += means

    $AllMembers = $AllMembers + $objrow

  • #28104
    Profile photo of Graham Pinkston
    Graham Pinkston
    Participant

    oh snap... my code is correct with `$AllMembers += $objrow'
    I must have copied and pasted that wrong somehow.

    My issue is I end up with all the objects containing the same information. The loop is overwriting all objects in the array ... but I don't see why.

    kvprasoon.. I thank you for trying to help, but I don't think you see what I'm trying to do here.

    The goal is get a list of groups based on a seachbase (which I have).
    Get the members from that group (all members, not just users)
    Add the info I need to an object (Member Name, Member Email, Member GUID, Group Name, and Group Email).
    My script currently appends this info to a CSV file. I have other functions to preform so it seems a waste to import this CSV file back in... so I would like to have an array of objects to work with.

    Anyway, thank you and I'm still working on it. Any help is greatly appreciated.

  • #28105
    Profile photo of kvprasoon
    kvprasoon
    Participant

    Hi Graham,
    Get-Aduser will accept Userobjects as -identity,For Group We Should use Get-AdGroup
    And I couldn't find a way to append multiple values to psobject,So better use an array

    
    Write-Verbose -Message ":::::::::::::: Starting Script  ::::::::::::::"
    
       $AllMembers = @()
        $Groups = Get-ADGroup -Properties Mail -Filter * -SearchBase $StoreLocation
        
        ForEach ($Group in $Groups) { 
            $Members = Get-ADGroupMember -Identity $Group 
            ForEach ($Member In $Members) {
    # Using Get-ADuser for GroupObjects
            if($member.objectClass -ieq "User"){
                $User = Get-ADUser -Identity $Member}
                elseif($member.objectClass -ieq "Group"){
    # using Get-ADGroup for Group Objects
                $Grp = Get-ADgroup -Identity $Member}
                else{}
                $UserName = $Member.SamAccountName
                $allmembers+=new-object psobject -Property @{
                MemberName = $Member.SamAccountName;
                MemberEmail = $User.UserPrincipalName;
                MemberGUID = $Member.objectGUID;
                GroupName = $Grp.SamAccountName;
                GroupEmail = $Grp.Mail;
                }
                Write-Verbose -Message "Adding member $UserName to objarray..."
                          
            }
        }
    

You must be logged in to reply to this topic.