expanding distribution group nested function

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mdlister
Posts: 5
Last visit: Fri Jun 30, 2023 7:29 am

expanding distribution group nested function

Post by mdlister »

Afternoon,

I'm having some trouble with a nested foreach loop and returning the output to an array created outside of the function. It works while i debug it but the final output is empty. I've missed something and im sure its obvious but i can't see my error.
I know i can do a recursive lookup instead but that wont tell me which group they are a member of that is giving them the access.

any help is greatly appreciated.

Update:- Not sure how to get the codebox working, i have attached the script and referenced it.

$objectCollection=@()

Function Expand-Group ($groupname)
{
$members = Get-ADGroupMember -Identity $GroupName
foreach ($member in $members)
{
$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name GroupName -Value $GroupName

if ($member.ObjectClass -eq "Group")
{
Add-Member -InputObject $object -MemberType NoteProperty -Name user -Value ($member.distinguishedName)
$objectCollection += $object
Write-host "Group Found $member"
Expand-Group $member.distinguishedName
}
else
{
Write-host "User Found $member"
Add-Member -InputObject $object -MemberType NoteProperty -Name user -Value ($member.distinguishedName)
$objectCollection += $object
}

}
}

Expand-group "exchange admin"

$objectCollection
Attachments
ExpandGroups.ps1
(1.26 KiB) Downloaded 107 times
.: Lister :.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: expanding distribution group nested function

Post by jvierra »

You must return the collection from the function for it to exist outside of the function.
User avatar
mdlister
Posts: 5
Last visit: Fri Jun 30, 2023 7:29 am

Re: expanding distribution group nested function

Post by mdlister »

I thought defining a variable or array outside of a function would allow the function to push information in to it?

I have found by declaring it as a global $global:objectCollection=@()

and then referencing it later $global:objectCollection += $object now gives me the output i require.
.: Lister :.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: expanding distribution group nested function

Post by jvierra »

That is not normally how we use functions. In fact it is considered bad coding to use globals to avoid proper scoping of variables and correct design of functions.

Read the following very carefully:

help about_scope

Next search for posts on how to design and use functions.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked