Help with Custom Object as output of Function

Welcome Forums General PowerShell Q&A Help with Custom Object as output of Function

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
8 months, 2 weeks ago.

  • Author
    Posts
  • #133845

    Participant
    Topics: 6
    Replies: 4
    Points: 75
    Rank: Member

    Hi All,

    I have been using a script for some time now to grab all the groups a user is a member of in AD, as well as details of the group in question. It works fine and I have managed to adjust it to accept a custom object as input and spit out Microsoft.ActiveDirectory.Management.ADGroup objects as a result.

    The issue I have is if my input object contains more then one user I can not differentiate the results from one user to the next and ideally would like to create an object that can be used again later. I believe I can resolve this by creating a custom object as the ouput which lists the user, domain and then group objects I receive but I am unsure how to go about this.

    Function Get-AllADGroups ()
    {
    
    [cmdletBinding()]
    Param
    (
    [Parameter(ValueFromPipelineByPropertyName, Position=0, Mandatory=$true)]
    [string]$Identity,
    [Parameter(ValueFromPipelineByPropertyName, Position=1, Mandatory=$false)]
    [string]$Server= "my.domain.com"
    )
    
    process
    {
    
    #Write-Output "User is: $Identity"
    #Write-Output "Domain is: $Server"
    
    foreach ($Group in (Get-ADPrincipalGroupMembership -Identity $Identity -Server $Server | Select-Object name))
    {
    Get-ADGroup -Identity $Group.name -Server $Server -Properties * #| Select-Object name, description
    }
    
    #Write-Output ""
    }
    }
    
    #Below code is to create an example of the in going custom object
    $test = @([pscustomobject]@{Identity="User1";Server="my.domain.com"},
    [pscustomobject]@{Identity="User1";Server="my.domain2.com"},
    [pscustomobject]@{Identity="User2";Server="my.domain.com"},
    [pscustomobject]@{Identity="User2";Server="my.domain2.com"})
    
    $test
    
  • #133856

    Participant
    Topics: 1
    Replies: 1532
    Points: 2,601
    Helping Hand
    Rank: Community Hero

    What about something like this?

    Function Get-AllADGroups (){
        [cmdletBinding()]
        Param(
            [Parameter(ValueFromPipelineByPropertyName, Position=0, Mandatory=$true)]
            [string]$Identity,
            [Parameter(ValueFromPipelineByPropertyName, Position=1, Mandatory=$false)]
            [string]$Server= "my.domain.com"
        )
        process{
            foreach ($Group in (Get-ADPrincipalGroupMembership -Identity $Identity -Server $Server | Select-Object name)){
                $Group = Get-ADGroup -Identity $Group.name -Server $Server -Properties Name, Description
                [PSCustomObject]@{
                    Identity = $Identity
                    GroupName = $Group.Name
                    GroupDescription = $Group.Description
                }
            }
        }
    }
  • #133869

    Participant
    Topics: 8
    Replies: 17
    Points: 64
    Rank: Member

    Hi, I would do something like this:

    So you can call the function like this:

    
    . .\Get-AllADGroups.ps1
    
    Get-AllADGroups -Identity 'usera'
    
    # or with multiple users:
    
    Get-AllADGroups -Identity @('usera'. 'userb')
    
    # or even like this:
    
    @('usera', 'userb') | Get-AllADGroups
    
    

    The output is a collection of the same custom objects with your custom type name 😀

    • #133908

      Participant
      Topics: 6
      Replies: 4
      Points: 75
      Rank: Member

      Hi Marco, Thanks for the reply but, I think you may have inadvertantly deleted part of your post?

The topic ‘Help with Custom Object as output of Function’ is closed to new replies.