I am trying to make a script to create groups, user and OUs for my DC. It is supposed to add 250 users and have the first 60 placed in the RODC and then randomly split the other 250 into the OUs but after it has placed the first 60 users into the RODC OU it will then try to add the same users to other OUs with this error:
dsadd failed:cn=amohn1,ou=DB_Engineers,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM:The specified account already exists.
I am not sure why it is doing this. Here is my script:
$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.##
$iteration = 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($iteration -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"
$iteration++
$ComputerNumber++
}
##Resetting the iteration variable.##
$iteration = 0
##Resetting the random variable.##
$random = 0
while($iteration -ne 250) ##Begins loop for user creation.##
{
while($iteration -lt 60) ##Places first 60 users into the RODC OU.##
{
##Splits the values in the .txt file via spaces.##
$values = $filecontent[$iteration] -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.##
$iteration++
}
$values = $filecontent[$iteration] -split(' ')
##Random used to determine which OUs a user is put in.##
$random = Get-Random -Minimum 1 -Maximum 9
$usrname = $values[0]
##Sets iteration to 70 above zero so we have no repeat accounts.##
$iteration = 70
while($iteration -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.##
}
$iteration++
}
}
}else ##Computer is not on any of the above domains and script will exit.##
{
Write-Host "Domain not listed, exiting script."
}
Because you keep resetting your $iteration
variable to 70
!
During the first run of the outer while($iteration -ne 250)
loop:
while ($iteration -ne 250) {
# runs 60 times ($iteration -in 0..59)
while ($iteration -lt 60) {
$iteration++
}
# $iteration is reset (to a higher value)
$iteration = 70
# runs 110 times ($iteration -in 70..179)
while ($iteration -ne 180) {
$iteration++
}
}
Alright, so far so good - $iteration
is not 250, so the loop body runs again:
while ($iteration -ne 250) {
# $iteration is now 180, so we skip this loop
while ($iteration -lt 60) {
$iteration++
}
# $iteration is reset (this time to a lower value!)
$iteration = 70 # <-- endless loop detected
# runs another 110 times ($iteration -in 70..179)
while ($iteration -ne 180) {
$iteration++
}
}