# Set the domain names
$LocalDomain = "localdomain.local"
$TrustedDomain = "trusteddomain.local"
# Set the path to the trusted domain group
$TrustedDomainGroupPath = "LDAP://cn=TrustedDomainGroup,cn=Users,dc=trusteddomain,dc=local"
# Set the credentials for the trusted domain
$TrustedDomainCred = Get-Credential -UserName "trusteddomain\administrator" -Message "Enter the password for the trusted domain administrator account."
# Get the groups and members from the CSV file
$Groups = Import-Csv -Path "C:\Groups.csv"
# Loop through each group and add members
foreach ($Group in $Groups) {
$GroupName = $Group.GroupName
$GroupMembers = $Group.GroupMembers
# Get the distinguished name of the group
$GroupDN = (Get-ADGroup $GroupName).DistinguishedName
# Loop through each group member and add to group
foreach ($Member in $GroupMembers) {
$MemberType = $Member.MemberType
$MemberName = $Member.MemberName
$MemberDomain = $Member.MemberDomain
if ($MemberType -eq "Local") {
# Add member from the local domain
$LocalDomainUser = Get-ADUser -Identity $MemberName -Server $LocalDomain
Add-ADGroupMember -Identity $GroupDN -Members $LocalDomainUser
} elseif ($MemberType -eq "Trusted") {
# Add member from the trusted domain
$TrustedDomainContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, $TrustedDomain, $TrustedDomainCred.UserName, $TrustedDomainCred.GetNetworkCredential().Password)
$TrustedDomainUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($TrustedDomainContext, $MemberName)
$LocalDomainUser = Get-ADUser -Filter {Enabled -eq $true -and ObjectGUID -eq $TrustedDomainUser.Guid.ToByteArray()} -Server $LocalDomain
Add-ADGroupMember -Identity $GroupDN -Members $LocalDomainUser
} else {
Write-Warning "Invalid member type '$MemberType' for member '$MemberName' in group '$GroupName'. Skipping."
}
}
}
To use the script, save it as a .ps1 file and create a CSV file named “Groups.csv” with the following headers: GroupName, MemberType, MemberName, MemberDomain.
In the GroupName column, enter the name of the group you want to add members to. In the MemberType column, enter either “Local” or “Trusted” to indicate whether the member is from the local domain or the trusted domain. In the MemberName column, enter the username of the member. In the MemberDomain column, enter the domain name of the member (e.g. “localdomain.local” or “trusteddomain.local”).
Then, run the script in PowerShell and enter the password for the trusted domain administrator account when prompted. Note that you may need to adjust the domain names and trusted domain group path to fit your environment.