【问题标题】:Windows Server 2016: Bulk create user accounts in an OUWindows Server 2016:在 OU 中批量创建用户帐户
【发布时间】:2020-05-21 15:27:12
【问题描述】:

我对服务器管理非常陌生,我需要创建 500 个用户帐户。我为用户的详细信息创建了一个 .csv 文件。这是它的样子。

我已将此文件复制到我的虚拟机上,在我的 C: 驱动器中作为 hr-names.csv 在我的虚拟机上,这就是它的样子。

我需要在 G HR 组内创建所有用户。我已经打开了属性的属性并复制了路径

CN=G HR,OU=HR,OU=Zara Pte Ltd,DC=frontier,DC=net

我一直在尝试运行此代码,但无济于事。它不断在我的用户容器中创建用户,而不是在我的 G HR 组中。

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\hr-names.csv

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
    #Read user data from each field in each row and assign the data to a variable as below

    $Username   = $User.username
    $Password   = $User.password
    $Firstname  = $User.firstname
    $OU         = $User.ou #This field refers to the OU the user account is to be created in
    $Password = $User.Password


    #Check to see if the user already exists in AD
    if (Get-ADUser -F {SamAccountName -eq $Username})
    {
         #If user does exist, give a warning
         Write-Warning "A user account with username $Username already exist in Active Directory."
    }
    else
    {
        #User does not exist then proceed to create the new user account

        #Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@frontier.net" `
            -Name "$Firstname" 
            -GivenName $Firstname
            -Path $OU `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False 
    }
} 

它也会产生此错误,但偶尔会在用户容器内创建一些帐户:

我需要有关创建 powershell/cmd 代码的帮助,该代码可以帮助我使用我的 .csv 文件创建 500 个用户并允许所有用户属于 G HR 组。

编辑:@Rup 向我指出,我需要先将用户添加到 OU 中,然后才能将它们添加到组中。但是,我仍然不确定如何将我的用户添加到我的 HR OU。它们被添加到用户容器中。

编辑 2:我不知道如何立即将我的用户添加到我的 G HR 组。尝试了@Rup 为我手动创建但不起作用的其他用户提供的链接

【问题讨论】:

  • OU 是在 AD 中创建用户对象的路径。如果你想让他们成为一个组的成员,那就不是一回事了。
  • @Rup 那么我应该在 OU 中创建它,然后使用单独的 powershell 代码将其添加到组中吗?
  • @Rup 目前所有用户都在用户组内创建,甚至在 hr 组内。无论如何我可以改变它吗?
  • “正在用户组内创建用户” - 这不是发生的事情。不能在组中“创建”用户。用户是在 OU 中创建的。然后将现有用户添加到组中。

标签: windows powershell active-directory virtual-machine


【解决方案1】:

您在 -Name 和 -GiveName 上缺少 `,这告诉 powershell 即使在单独的行中,New-ADUser cmdlet 的一部分仍然不是新的 cmdlet。您也可以将所有 New-ADUser 都放在一行代码中,如下所示;

New-ADUser -SamAccountName $Username -UserPrincipalName "$Username@frontier.net" -Name "$Firstname" -GivenName $Firstname -Path $OU -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False

或者使用下面的代码

New-ADUser `
    -SamAccountName $Username `
    -UserPrincipalName "$Username@frontier.net" `
    -Name $Firstname `
    -GivenName $Firstname `
    -Path $OU `
    -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False 

}

希望对你有帮助

【讨论】:

  • 这只是第一个问题。修复此问题后,将给出不同的错误,因为传递给 -Path 的值不是 OU。
  • @GabrielLuci 那么我如何确保我的路径是正确的?应该只是 OU=HR,OU=Zara Pte Ltd,DC=frontier,DC=net 吗?
  • 如果这是您希望他们加入的 OU。只有您可以回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2021-01-17
  • 1970-01-01
相关资源
最近更新 更多