Help with Custom Object as output of Function

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.

[pre]
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
[/pre]

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
}
}
}
}

Hi, I would do something like this:

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

So you can call the function like this:

[pre]

. .\Get-AllADGroups.ps1

Get-AllADGroups -Identity ‘usera’

or with multiple users:

Get-AllADGroups -Identity @(‘usera’. ‘userb’)

or even like this:

@(‘usera’, ‘userb’) | Get-AllADGroups

[/pre]

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

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