【发布时间】: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