我有一个csv文件,正在使用该文件将多个用户上载到Active Directory。其中的“组”列将包含多个不同的条目。我希望能够将其作为新用户脚本的一部分。
在创建用户以将组应用于每个用户之后,我当前运行第二个脚本。
# 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:\upload\testbulkupload2.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
$Lastname = $User.lastname
$OU = $User.ou #This field refers to the OU the user account is to be created in
$email = $User.email
$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@lon.cloud" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False -PasswordNeverExpires:$True
}
}
然后我跑
$user = 'username'
$groups = 'DRC VPN Users', 'Echo Prod Users'
foreach ($group in $groups) {
Add-ADGroupMember -Identity $group -Members $user
}
我正试图将它们作为一个脚本获得,但结合起来并没有运气。
答案 0 :(得分:0)
New-ADUser
cmdlet有很多参数,但是没有用于设置组成员身份的参数。
如下创建用户后,需要完成此操作。
我对代码进行了一些更改,并使用splatting以获得更好的阅读效果,并避免使用难以理解的反引号。 同样,也无需先将CSV文件中的所有字段捕获到变量中。只需将它们直接用作参数即可。
# 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:\upload\testbulkupload2.csv
$groups = 'DRC VPN Users', 'Echo Prod Users'
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
# Read user data from each field in each row
# the username is used more often, so to prevent typing, save that in a variable
$Username = $User.username
# 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
# create a hashtable for splatting the parameters
$userProps = @{
SamAccountName = $username
UserPrincipalName = '{0}@lon.cloud' -f $username
GivenName = $User.firstname
Surname = $User.lastname
Name = '{0} {1)' -f $User.firstname, $User.lastname
DisplayName = '{0}, {1}' -f $User.lastname, $User.firstname
Path = $User.ou # This field refers to the OU the user account is to be created in
EmailAddress = $User.email
AccountPassword = (ConvertTo-SecureString $User.password -AsPlainText -Force)
Enabled = $true
ChangePasswordAtLogon = $false
PasswordNeverExpires = $true
}
New-ADUser @userProps
# add the new user to the groups here
foreach ($group in $groups) {
Add-ADGroupMember -Identity $group -Members $username
}
}
}
请注意,Add-ADGroupMember
可以在-Members
参数中使用用户名数组来批量添加用户,但是在这种情况下,我建议您这样做以避免用户收到错误消息已经存在于AD中(因此您没有在上面的代码中创建它们)