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
expanding distribution group nested function
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.
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.
expanding distribution group nested function
- Attachments
-
- ExpandGroups.ps1
- (1.26 KiB) Downloaded 107 times
.: Lister :.
Re: expanding distribution group nested function
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.
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 :.
Re: expanding distribution group nested function
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.
Read the following very carefully:
help about_scope
Next search for posts on how to design and use functions.