Я пытаюсь создать скрипт для создания групп, пользователей и подразделений для моего D C. Предполагается добавить 250 пользователей и поместить первые 60 пользователей в ROD C, а затем случайным образом разделить остальные 250 на подразделения, но после размещения первых 60 пользователей в подразделении ROD C он попытается добавить тех же пользователей в другие подразделения с этой ошибкой:
Ошибка dsadd: cn = amohn1, ou = DB_Engineers, ou = программисты, ou = BRAAN, dc = BRAAN, dc = COM: указанная учетная запись уже существует.
Я не уверен, почему он это делает. Вот мой сценарий:
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent ##Grabs the directory path of the script to be used as a relevant path.##
$domainname = (Get-ADDomain -Current LocalComputer).NetBIOSName ##Grabs the computers domainname##
$filecontent = Get-Content $scriptDir\Users.txt ##Reads the .txt file##
##Setting global variables.##
$itteration = 0
$AdminCount = 0
$ComputerName = "default"
$ComputerNumber = 1
$ComputerType = "default"
$ServicePack = ""
if($domainname -eq "BRAAN") ##Is the computer on the BRAAN domain?##
{
##Build OU and Group Structure inside the domain.##
dsadd ou "ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=CustomDom,dc=BRAAN,dc=COM"
dsadd ou "ou=BRAANComputers,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=IT,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=HR,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Sales,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Executive,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=DB_Engineers,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Content_Experts,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Animators,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd ou "ou=Operations,ou=CustomDom,dc=BRAAN,dc=COM"
dsadd ou "ou=RODC,dc=BRAAN,dc=COM"
dsadd group "cn=IT_Staff,ou=IT,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=HR_Staff,ou=HR,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=Sales_Staff,ou=Sales,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=Executive_Staff,ou=Executive,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=DB_Engineers_Staff,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=Content_Experts_Staff,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=Animators_Staff,ou=BRAAN,dc=BRAAN,dc=COM"
dsadd group "cn=Operations_Staff,ou=Operations,ou=CustomDom,dc=BRAAN,dc=COM"
dsadd group "cn=RODC_Staff,ou=RODC,dc=BRAAN,dc=COM"
##Sets the computer name##
$ComputerName = "Braan-"
while($itteration -ne 80) ##While loop used to create ComputerObjects.##
{
$random = Get-Random -Minimum 1 -Maximum 3 ##Random number used to randomly assign OperatingSystem to the computers.##
if($random -eq 1)
{
$ComputerType = "Windows 10"
}
##Creation of PCs.##
$Computer = $ComputerName+$ComputerNumber
New-ADComputer -Name $Computer -OperatingSystem $ComputerType -OperatingSystemServicePack $ServicePack -Path "ou=BRAANComputers,ou=BRAAN,dc=BRAAN,dc=COM"
$itteration++
$ComputerNumber++
}
##Resetting the itteration variable.##
$itteration = 0
##Resetting the random variable.##
$random = 0
while($itteration -ne 250) ##Begins loop for user creation.##
{
while($itteration -lt 60) ##Places first 60 users into the RODC OU.##
{
##Splits the values in the .txt file via spaces.##
$values = $filecontent[$itteration] -split(' ')
$usrname = $values[0]
dsadd user "cn=$usrname,ou=RODC,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=RODC_Staff,ou=RODC,dc=BRAAN,dc=com" ##Create user and add to group.##
$itteration++
}
$values = $filecontent[$itteration] -split(' ')
##Random used to determine which OUs a user is put in.##
$random = Get-Random -Minimum 1 -Maximum 9
$usrname = $values[0]
##Sets itteration to 70 above zero so we have no repeat accounts.##
$itteration = 70
while($itteration -ne 180)##Begins loop for user creation.##
{
if($random -eq 1) ##IT OU.##
{
dsadd user "cn=$usrname,ou=IT,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=IT_Staff,ou=IT,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 2) ##HR OU.##
{
dsadd user "cn=$usrname,ou=HR,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=HR_Staff,ou=HR,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 3) ##Sales OU.##
{
dsadd user "cn=$usrname,ou=Sales,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=Sales_Staff,ou=Sales,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 4) ##Finance OU.##
{
dsadd user "cn=$usrname,ou=Finance,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=Finance_Staff,ou=Finance,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 5) ##Executives OU.##
{
dsadd user "cn=$usrname,ou=Executives,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=Executives_Staff,ou=Executives,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 6) ##DB_Engineers Group.##
{
dsadd user "cn=$usrname,ou=DB_Engineers,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=DB_Engineers_Staff,ou=DB_Engineers,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}elseif($random -eq 7) ##Content_Experts Group.##
{
dsadd user "cn=$usrname,ou=Content_Experts,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=Content_Experts_Staff,ou=Content_Experts,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}
elseif($random -eq 8)##Animators Group.##
{
dsadd user "cn=$usrname,ou=Animators,ou=BRAAN,dc=BRAAN,dc=COM" -fn $values[1] -ln $values[2] -disabled no -pwd "Password1" -memberof "cn=Animators_Staff,ou=Animators,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=com" ##Create user and add to group.##
}
##Checks if admin accounts are needed, if so then adds one admin account given that they are also in the Management OU.##
if($AdminCount -ne 2 -and $random -eq 2)
{
$AdminCount++
dsmod group "cn=Domain Admins,cn=Users,dc=BRAAN,dc=COM" -addmbr "cn=$usrname,ou=Management,ou=BRAAN,dc=BRAAN,dc=com" ##Adds account to Domain Admins.##
dsmod group "cn=Enterprise Admins,cn=Users,dc=BRAAN,dc=COM" -addmbr "cn=$usrname,ou=Management,ou=BRAAN,dc=BRAAN,dc=com" ##Adds account to Enterprise Admins.##
}
$itteration++
}
}
}else ##Computer is not on any of the above domains and script will exit.##
{
Write-Host "Domain not listed, exiting script."
}