Required details of specific Groups existing in multiple domains in active directory

AJU 0 Reputation points
2023-08-07T21:44:10.1233333+00:00

Please help me with the script to fetch the details of specific groups existing in multiple domains in active directory. Details required are created, Description, GroupScope, groupType, Memberof, SamAccountName, ManagedBy.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-08-08T16:10:47.5+00:00
    Hello AJU,
    
    Thank you for your question and for reaching out with your question today.
    
    To fetch the details of specific groups existing in multiple domains in Active Directory using PowerShell, you can use the following script as a starting point. This script assumes that you have the Active Directory PowerShell module installed and you have the necessary permissions to query group information across domains.
    
    ```powershell
    # List of domains
    $domains = @("domain1.com", "domain2.com")
    
    # List of group names to fetch details for
    $groupNames = @("Group1", "Group2")
    
    # Loop through each domain
    foreach ($domain in $domains) {
        Write-Host "Domain: $domain"
        
        # Connect to the domain
        Import-Module ActiveDirectory
        Set-Location AD:
        $domainContext = Get-ADDomain $domain
        
        # Loop through each group
        foreach ($groupName in $groupNames) {
            $group = Get-ADGroup -Filter {Name -eq $groupName} -Server $domainContext.DistinguishedName
    
            if ($group) {
                Write-Host "Group: $($group.Name)"
                Write-Host "Created: $($group.Created)"
                Write-Host "Description: $($group.Description)"
                Write-Host "GroupScope: $($group.GroupScope)"
                Write-Host "GroupType: $($group.GroupType)"
                Write-Host "SamAccountName: $($group.SamAccountName)"
                
                # Get member of groups
                $memberOf = Get-ADGroup -Filter {Member -eq $group.DistinguishedName} -Server $domainContext.DistinguishedName
                if ($memberOf.Count -gt 0) {
                    Write-Host "MemberOf:"
                    foreach ($memberOfGroup in $memberOf) {
                        Write-Host "  $($memberOfGroup.Name)"
                    }
                } else {
                    Write-Host "MemberOf: None"
                }
    
                $managedBy = $group.ManagedBy
                if ($managedBy) {
                    $managedByGroup = Get-ADGroup $managedBy
                    Write-Host "ManagedBy: $($managedByGroup.Name)"
                } else {
                    Write-Host "ManagedBy: Not specified"
                }
    
                Write-Host
            } else {
                Write-Host "Group '$groupName' not found in $domain"
            }
        }
    }
    

    Please replace the domain names and group names in the script with your actual domain names and group names. Keep in mind that this script should be executed with administrative privileges and that you need appropriate permissions to query group information in the specified domains. Additionally, you might need to adjust the script to fit your specific environment and requirements.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Best regards.

    
    
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2023-08-09T02:40:42.5466667+00:00

    See if this is what you need:

    $gc = (Get-ADDomainController -Discover -Service GlobalCatalog).hostname[0]
    $gcport = 3268      # or 3269 if you want, or need, to use a secure SSL connection
    $gcserver = "{0}:{1}" -f $gc, $gcport
    $gtype = @{
        2           = 'Distribution Group'
        4           = 'Distribution Group'
        8           = 'Distribution Group'
        -2147483646 = 'Security Group'
        -2147483644 = 'Security Group'
        -2147483640 = 'Security Group'
    }
    Get-Content c:\junk\groups.txt |
        ForEach-Object{
            $gdn = (Get-ADGroup $_ -Server $gcserver).distinguishedName     # get group from GC
            $gr = Get-ADGroup $gdn -Properties created, Description, GroupScope, groupType, Memberof, SamAccountName, ManagedBy      # get group propertied from DC
            $mo = ""
            if ($gr.memberOf.count -gt 0){
                $membof = @()
                foreach ($m in $gr.memberof){
                    $membof += (Get-ADGroup $m).Name
                }
                $mo = $membof -join ';'
            }
            $mgby = ""
            if ($gr.ManagedBy){
                $mgBy = (Get-ADObject $gr.ManagedBy).Name
            }
            [PSCUstomObject]@{
                Created = ($gr.created.ToString("MM/dd/yyyy HH:mm:ss"))
                Description = $gr.Description
                GroupScope = $gr.GroupScope
                GroupType = $gtype.($gr.GroupType)
                MemberOf = $mo
                SamaccountName = $gr.SamAccountName
                ManagedBy = $mgBy
            }
        }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.