将所有组从AD用户复制到另一个用户

时间:2017-11-27 07:47:23

标签: powershell active-directory

在我工作的公司中有2个域名。第一个是主域,还有另一个具有信任关系的子域。 我尝试创建一个PowerShell脚本,当我们创建新用户时,我们可以设置源用户名并将他所属的所有组复制到新用户。

对于每个用户,都有来自主域和辅助域的组

我在PowerShell中创建了一个脚本来复制组,但如果新用户在子域中,那么将要添加的唯一组是子域中的组,如果他在主域中,他将被添加到的组是在主域。

如何确保添加所有组?

import-Module ActiveDirectory

# Collect the source and target users

$Source = Read-Host "Enter logon name of source user"
$Target = Read-Host "Enter logon name of target user"

# Set the domain for the user

$domain_Source=$null

$domainval = Read-Host -Prompt 'Enter 1 for lpnet 2 for Sub-DB for the Source 
username'

if ($domainval -eq 1)
{
$domain_Source = "main.lpnet.com"
}
if ($domainval -eq 2)
{
$domain_Source = "sub-db.lpnet.com"
}



Get-ADUser -Identity $Source -Server $domain_Source -Properties memberof |
Select-Object -ExpandProperty memberof|
Add-ADGroupMember -Members $Target -PassThru | 
Select-Object -Property SamAccountName

0 个答案:

没有答案