Get-ADGroup - group name, name and address of ManagedBy

I am looking to get a name-driven group and email-driven group name in a PowerShell query like this.

Get-ADGroup -filter {Name -like "*Admins" } 

The result will look something like this:

 Group Name | Managed By Name | Managed By Email 

The problem I encountered is related to Get-ADGroup and Get-ADUser . In SQL, this “connection” will occur at get-adgroup.managedby = get-aduser.distinguishedname . I know that this is not how Powershell works, I just thought that I would throw away an example of what I'm trying to do.

Any help would be appreciated and appreciated.

+5
source share
3 answers

I see that @Mathias R. Jessen beat me, but here is what I had:

 Get-ADGroup -filter {Name -like "*IT*" } -Properties managedBy | ForEach-Object { ` $managedBy = $_.managedBy; if ($managedBy -ne $null) { $manager = (get-aduser -Identity $managedBy -Properties emailAddress); $managerName = $manager.Name; $managerEmail = $manager.emailAddress; } else { $managerName = 'N/A'; $managerEmail = 'N/A'; } Write-Output $_; } | Select-Object @{n='Group Name';e={$_.Name}}, @{n='Managed By Name';e={$managerName}}, @{n='Managed By Email';e={$managerEmail}} 
+4
source

You can do it as follows:

 $Groups = Get-ADGroup -Filter { Name -like "*Admins" } -Properties managedBy,mail $Groups |Select-Object Name,@{Name='ManagedBy';Expression={(Get-ADUser $_.managedBy).Name}},Mail 

The syntax @{} after Select-Object is known as a computed property .


You can also pass groups to ForEach-Object and call Get-ADUser inside the process script:

 Get-ADGroup -Filter {Name -like "*Admins"} -Properties managedBy,mail |ForEach-Object { # Find the managedBy user $GroupManager = Get-ADUser -Identity $_.managedBy # Create a new custom object based on the group properties + managedby user New-Object psobject -Property @{ Name = $_.Name ManagedBy = $GroupManager.Name Email = $_.mail } } 
+2
source

I would do something like this:

 # Get all groups into a variable $Group = Get-ADGroup -Filter {Name -like "*Admin*"} | Select-Object -expandProperty Name foreach ($Groups in $Group){ # Get ManagedBy name for each group (If ManagedBy is empty group wil not be listed) $User = Get-ADGroup $Groups -Properties * | Select-Object -ExpandProperty ManagedBy foreach ($Users in $User){ # Get Name and EmailAddress for each User $Name = Get-ADUser $Users | Select-Object -ExpandProperty Name $Email = Get-ADUser $Users -Properties * | Select-Object -ExpandProperty EmailAddress # Write output Write-Host $Groups "," $Name "," $Email } } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1241539/


All Articles