Help with Custom Object as output of Function

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

Viewing 2 reply threads
  • Author
    Posts
    • #133845
      Participant
      Topics: 7
      Replies: 5
      Points: 88
      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: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      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: 66
      Rank: Member

      Hi, I would do something like this:

      <script src=”https://gist.github.com/OCram85/4ddb27ca34c7b5cc2e1a3363ea0eb463.js”></script&gt;

      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: 7
        Replies: 5
        Points: 88
        Rank: Member

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

Viewing 2 reply threads
  • The topic ‘Help with Custom Object as output of Function’ is closed to new replies.