Ingweil
September 16, 2022, 8:44am
1
Hello,
I am looking from this beginning of the script to use the result with a Foreach to list the Inherited groups:
Param (
[String]$Name =“”
)
While (!$Name)
{
$Name = Read-Host “Nom du compte”
}
$Name = Get-ADUser $Name -Properties *
$Name.Name
$Name.Memberof
The goal is to have the list of groups to which a user belongs but also the list of groups he inherited.
If anyone has any idea how I could do this I’m all ears…
Thanks in advance to anyone who can help me
Olaf
September 16, 2022, 9:02am
2
Mike,
Welcome to the forum.
Please … When you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance
How to format code in PowerShell.org <---- Click
You need to query all groups the user belongs to recursivly for their members where the objectClass is equal to group
But … as always … most of the time you’re not the first one with a given task. Please use your favorite internet search engine to search for examples you can adapt to your particular needs. You don’t need to re-invnet the wheel again.
Ingweil
September 16, 2022, 9:59am
3
Hi Olaf and thanks for your answer,
I have been browsing the net for two long days looking for information but unfortunately I have not found anything that has allowed me to move forward.
Olaf:
Please … When you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Very sorry i take the advice !
Olaf
September 16, 2022, 10:54am
4
If I take your subject, add “PowerSehll” to it and search for that I actually find enough promissing results:
https://www.google.com/search?q=powershell+list+AD+groups+and+inherited+AD+groups+for+a+User
Ingweil
September 19, 2022, 6:57am
5
Hi !
Thank you for your answer it allowed me to move forward. Here’s my script right now:
Param ([String]$Name =“”)
While (!$Name)
{
$Name = Read-Host “Nom du compte”
}
$Name = Get-ADUser $Name -Properties *
#$Name.Name
#$Name.Memberof
#Get all recursive groups a user belongs.
Function Get-ADUserNestedGroups
{
Param
(
[string]$DistinguishedName,
[array]$Groups = @(),
$AD = @()
)
If (!$AD) {$AD = Get-ADObject -Filter * -Properties *}
#Get the AD object, and get group membership.
#$ADObject = Get-ADObject -Filter “DistinguishedName -eq ‘$DistinguishedName’” -Properties memberOf, DistinguishedName
$ADObject = $AD | Where-Object DistinguishedName -eq $DistinguishedName
#If object exists.
If($ADObject)
{
#Enummurate through each of the groups.
Write-Host ($ADObject.DistinguishedName + “-”)
Foreach($GroupDistinguishedName in $ADObject.memberOf)
{
#Get member of groups from the enummerated group.
#$CurrentGroup = Get-ADObject -Filter “DistinguishedName -eq ‘$GroupDistinguishedName’” -Properties memberOf, DistinguishedName
$CurrentGroup = $AD | Where-Object DistinguishedName -eq $GroupDistinguishedName
#Check if the group is already in the array.
If(($Groups | Where-Object {$_.DistinguishedName -eq $GroupDistinguishedName}).Count -eq 0)
{
#Add group to array.
$Groups += $CurrentGroup
#Get recursive groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups -AD $AD
}
}
}
#Return groups.
Return $Groups;
}
#Get all groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Identity $name).DistinguishedName;
#Output all groups.
$Groups | Select-Object Name | Sort-Object -Property Name; >
What do you think about it?
Olaf
September 19, 2022, 7:11am
6
??
Does it do what you need?
Ingweil
September 19, 2022, 7:59am
7
Indeed the script comes out to me well the desired results, on the other hand I want to make them stand out in text file and can be simplify the script itself.
Olaf
September 19, 2022, 8:55am
8
You still did not format your code as code. You can update your existing post and fix it.
What text file? In the code you share I cannot see any reference to a text file. For structured data I’d recommend to use CSV files anyway.
Ingweil
September 20, 2022, 12:40pm
9
hello, I fixed the script could you tell me if for you it is more readable?
I’m working to structured a CSV files !
I appreciate you trying, but still isn’t quite right. It looks like you “quoted” your code versus using the preformatted option </>
I also hope your code is indented as what you’re showing here is not. Did you also have some sample CSV lines?