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 2 replies, has 2 voices, and was last updated by

1 week ago.

  • Author
  • #133845

    Points: 60
    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 ()
    [Parameter(ValueFromPipelineByPropertyName, Position=0, Mandatory=$true)]
    [Parameter(ValueFromPipelineByPropertyName, Position=1, Mandatory=$false)]
    [string]$Server= ""
    #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 $ -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=""},
  • #133869

    Points: 42
    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

      Points: 60
      Rank: Member

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

You must be logged in to reply to this topic.