【发布时间】:2018-09-21 07:24:10
【问题描述】:
我需要从 AD Windows2003Forest 迁移到 AD 2016。我有以下脚本可以批量创建用户。我的要求是将旧 AD 的相同 SID 映射到新 AD。例如,在旧 AD 中 SID='xyz' 那么它在 newAD 中也应该与 SID='xyz' 相同
我拥有所有用户数据以及 CSV 格式的 SID,并且正在使用以下 PowerShell 脚本,但不知何故无法正常工作。作为意见或建议。
powershell 代码片段:
#Enter a path to your import CSV file
$ADUsers = Import-csv C:\scripts\newusers.csv
foreach ($User in $ADUsers)
{
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$Department = $User.department
$OU = $User.ou
$sid = $User.sid
$UserPrincipalName = $User.UserPrincipalName
$DistinguishedName = $User.DistinguishedName
#Check if the user account already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, output a warning message
Write-Warning "A user account $Username has already exist in Active Directory."
}
else
{
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName $UserPrincipalName `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-ChangePasswordAtLogon $True `
-DisplayName "$Lastname, $Firstname" `
-Department $Department `
-DistinguishedName $DistinguishedName `
-SID $sid `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
}
【问题讨论】:
-
您不能分配这样的 SID。这是article about SIDs,但重要的部分是 RID 主“FSMO”角色(意味着 DC 生成 SID)。添加到此 SID 中包含域信息,因此如果您有一个新域,则不能有相同的 SID。这很好,因为您可能希望在迁移期间使用 SID 历史记录,以便在重建时保留旧权限。我还建议您使用 Microsoft 的 AD 迁移工具,而不是尝试手动编写脚本。
标签: powershell active-directory active-directory-group