PowerShell Code

Remove-LAPSGPLink.ps1

View-only PowerShell code for review.

<#
.SYNOPSIS
    Retrieves the group memberships of specified Active Directory computer accounts.
.DESCRIPTION
    This script reads a list of server names from a text file and uses the Get-ADPrincipalGroupMembership cmdlet to retrieve the groups that each computer account is a member of in Active Directory. The results are displayed in the console.
.EXAMPLE
    .\Get-ADPrincipalGroup.ps1
    This command runs the script, which reads server names from "C:\servers.txt" and outputs the group memberships for each server to the console.  
.EXAMPLE
    Get-ADPrincipalGroup.ps1 | Out-File -FilePath "ServerGroupMemberships.txt"
    This command runs the script and saves the output to a text file named "ServerGroupMemberships.txt" in the current directory.
.INPUTS
    None. This script does not accept any input from the pipeline.
.OUTPUTS
    The script outputs the group memberships of each specified computer account, including the server name and the groups it belongs to.
.NOTES
    Ensure that the Active Directory module is installed and that you have the necessary permissions to query group memberships in your AD environment. 
    The script assumes that the server names are listed in a text file at "C:\servers.txt", with one server name per line. Adjust the file path as needed for your environment.
#>
# Import Active Directory module
Import-Module ActiveDirectory

# Read the list of servers from the text file
$servers = Get-Content -Path "C:\servers.txt"

# Loop through each server and get group memberships
foreach ($server in $servers) {
    # Get the computer object
    $computer = Get-ADComputer -Identity $server -ErrorAction SilentlyContinue
    
    if ($computer) {
        # Get the groups the computer is a member of
        $groups = Get-ADPrincipalGroupMembership -Identity $computer
        
        # Output the results
        if ($groups) {
            Write-Host "Server: $server"
            $groups | ForEach-Object { Write-Host " - Group: $($_.Name)" }
        } else {
            Write-Host "Server: $server has no group memberships."
        }
    } else {
        Write-Host "Server: $server not found."
    }
}