我可以在 Powershell 中使用 CSV 创建用户帐户。
我还可以从 Powershell 中的模板帐户创建帐户,并且地址和组成员身份等信息可以正常显示。
但是,当尝试在 foreach 循环内组合这两个操作时,模板帐户中的附加信息将不会填充到从 CSV 创建的新帐户中。
我尝试使用变量来捕获模板用户属性,包括使用变量仅捕获模板帐户中的组信息。
我还尝试使用帐户的特定名称而不是变量。 但是,模板帐户中的数据不会转移到 foreach 循环中从 CSV 创建的帐户。
您可以在下图中看到,数据永远不会填充到新创建的用户中。 (我不知道为什么图像没有显示,它向我显示了缩略图并给了我“添加图像”的选项)
您可以在下面的代码中看到我尝试过的事情:
#------------------------- Create users with CSV & Template -------------------------------
# Goal = to get all settings from template account to include city, address, & groups
# into each account that is being created from the CSV.
# The accounts create, but the other data (address & groups) do not populate.
# Import the CSV file
$users = Import-Csv -Path "C:\Users\Administrator\Desktop\Posh_Server2022\AD\Users.csv"
# Get the template user
$templateUser = Get-ADUser -Identity "Template User"
# Loop through each user in the CSV file
foreach ($user_ in $users) {
# Create the new user based on the template
New-ADUser -Name $user_.Name `
-GivenName $user_.GivenName `
-Surname $user_.Surname `
-SamAccountName $user_.UserName `
-AccountPassword $user_.Password `
-Instance $templateUser
# Get the groups that the template user is a member of
#$groups = Get-ADPrincipalGroupMembership -Identity $templateUser
############## THIS IS WHERE PROBLEMS START ##############
#$tmpltUsr = Get-ADUser 'Template User' -Properties MemberOf
#$tmpltUsr = Get-ADUser 'Template User' # THIS GIVES NO ERRORS BUT DOESN'T WORK
#$tmpltUsr = Get-ADPrincipalGroupMembership -Identity "Template User"
#ForEach($group in 'Template User'.MemberOf){
# ForEach($group in $groups){
ForEach($group in $templateUser.MemberOf){ # THIS GIVES NO ERRORS BUT DOESN'T WORK
# ForEach($group in $templateUser){
Add-ADGroupMember $group -Members $user_ #THIS GAVE NO ERRORS
#Add-ADGroupMember -Identity $group.Name -Members $user_
}
}
################################################## ##
CSV:
姓名、名字、姓氏、用户名、账户密码
测试用户1,测试,用户1,tuser1,密码
测试用户2,测试,用户2,tuser2,密码
正如 iRon 评论的那样,您不会 capture New-ADUser 创建的对象,您的代码现在使用从 CSV 读取的
PsCustomObject
作为参数 -Members
的值,但这不起作用。此外,我建议不要使用那些可怕的反引号,而是在可能需要大量参数的 cmdlet 上使用 Splatting。
尝试
#------------------------- Create users with CSV & Template -------------------------------
# Import the CSV file
$csvUsers = Import-Csv -Path "C:\Users\Administrator\Desktop\Posh_Server2022\AD\Users.csv"
# Get the template user (your code shows the Name only, not the SamAccountName which would be a lot easier)
# Get-ADUser by default returns these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# If you need extra user properties, you must provide them in parameter `Properties
$propsToCopy = 'StreetAddress', 'City', 'State', 'PostalCode', 'POBox'
$templateUser = Get-ADUser -Filter "Name -eq 'Template User' -or DisplayName -eq 'Template User'" -Properties MemberOf, $propsToCopy
if (!$templateUser) {
throw "Cannot find the template user.."
}
# Loop through each user in the CSV file and collect the user objects in $newUsers
$newUsers = foreach ($user in $csvUsers) {
# Create the new user based on the template
$userParams = @{
Name = $user.Name
GivenName = $user.GivenName
Surname = $user.Surname
SamAccountName = $user.UserName
AccountPassword = $user.Password
Instance = $templateUser
Enabled = $true
PassThru = $true # this makes the New-ADUser cmdlet output the ADPrincipal object it created
}
New-ADUser @userParams
}
# add the new users to the groups the Template User is a member of
foreach ($groupDN in $templateUser.MemberOf) {
Add-ADGroupMember -Identity $groupDN -Members $newUsers
}