gpt4 book ai didi

powershell - 多次创建用户

转载 作者:行者123 更新时间:2023-12-02 23:17:28 26 4
gpt4 key购买 nike

我正在尝试创建一个脚本来为DC创建组,用户和OU。它应该添加250个用户,并将前60个用户放置在RODC中,然后将其他250个用户随机拆分到OU中,但是在将前60个用户放置到RODC OU中之后,它将尝试将相同的用户添加到其他用户中出现此错误的OU:

dsadd failed:cn=amohn1,ou=DB_Engineers,ou=Programmers,ou=BRAAN,dc=BRAAN,dc=COM:The specified account already exists.



我不确定为什么要这样做。这是我的脚本:

$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."
}

最佳答案

因为您一直将$itteration变量重置为70!

在外部while($itteration -ne 250)循环的第一次运行期间:

while ($itteration -ne 250) {

# runs 60 times ($itteration -in 0..59)
while ($itteration -lt 60) {
$itteration++
}

# $itteration is reset (to a higher value)
$itteration = 70

# runs 110 times ($itteration -in 70..179)
while ($itteration -ne 180) {
$itteration++
}
}

好吧,到目前为止一切都很好- $itteration不是250,所以循环体再次运行:
while ($itteration -ne 250) {

# $itteration is now 180, so we skip this loop
while ($itteration -lt 60) {
$itteration++
}

# $itteration is reset (this time to a lower value!)
$itteration = 70 # <-- endless loop detected

# runs another 110 times ($itteration -in 70..179)
while ($itteration -ne 180) {
$itteration++
}
}

关于powershell - 多次创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60891438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com