我正在尝试通过 PowerShell AD 将我的用户添加到 AD 组。这是我当前的代码:
Import-Module ActiveDirectory #Import the active directory module
Import-CSV C:\Userlist.csv | ForEach { #Import the csv file and start the for each statement.
$groups =@{
grouparray = $_.group.split(',')
};
$user = @{ #Create the user variable and set the values within
name=$_.name #Call the name field from the csv file
givenname=$_.givenname #Callthe givenname field from the csv file.
surname=$_.surname #call the surname field from the csv file
samaccountname=$_.samaccountname #Call the samaccountname field from the csv file
department=$_.department #call the department field from the csv file.
accountpassword=(ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) #set the password
homedirectory=$_.homedirectory #Call the homedirectory field
emailaddress=$_.emailaddress #call the email address field
mobilephone=$_.mobilephone #call the mobile phone field
Path="Ou=People,dc=G3Zone,dc=local" #Path to the OU "People"
Enabled=$True #enable the account
};#@
New-ADUser @user #Create the new user with the information gathered fromthe csv.
add-ADGroupMember -Identity @groups –member $_.samaccountname
} #endforeach
这是我的 csv 文件:
name,givenname,surname,samaccountname,department,group,accountpassword,homedirectory,mobilephone,emailaddress
"Todd Fast",Todd,Fast,Tfast,President,ManagerGroup,P@ssword1,\\Group3\homedirs\Tfast,111-1111,Tfast@G3Zone.local
"Joe Doe",Joe,Doe,Jdoe,Accounting VP,"ManagerGroup,AccountingGroup",P@ssword1,\\Group3\homedirs\Jdoe,111-1112,JDoe@G3Zone.local
"Elaine Irving",Elaine,Irving,Eirving,HR VP,"ManagerGroup,HRGroup",P@ssword1,\\Group3\homedirs\Eirving,111-1113,EIrving@G3Zone.local
"Jane Malzur",Jane,Malzur,Jmalzur,Executive Assistant,"ManagerGroup, Corporate",P@ssword1,\\Group3\homedirs\Jmalzur,111-1114,JMalzur@G3Zone.local
Mike Fox,Mike,Fox,Mfox,IS VP,"ManagerGroup,ISGroup",P@ssword1,\\Group3\homedirs\Mfox,111-1115,MFox@G3Zone.local
Julie Cash,Julie,Cash,Jcash,Accounting,AccountingGroup,P@ssword1,\\Group3\homedirs\Jcash,111-1116,JCash@G3Zone.local
Manny Greene,Manny,Greene,Mgreene,Accounting,AccountingGroup,P@ssword1,\\Group3\homedirs\Mgreene,111-1117,MGreene@G3Zone.local
Russ Maine,Russ,Maine,Rmaine,HR,HRGroup,P@ssword1,\\Group3\homedirs\Rmaine,111-1118,RMaine@G3Zone.local
Paul Lam,Paul,Lam,Plam,HR,HRGroup,P@ssword1,\\Group3\homedirs\Plam,111-1119,PLam@G3Zone.local
Tom Scerbo,Tom,Scerbo,Tscerbo,HR,HRGroup,P@ssword1,\\Group3\homedirs\Tscerbo,111-1120,TScerbo@G3Zone.local
Kate McCool,Kate,McCool,KMcCool,HR,HRGroup,P@ssword1,\\Group3\homedirs\KMcCool,111-1121,KMcCool@G3Zone.local
Lech Walsh,Lech,Walsh,Lwalsh,IS,ISGroup,P@ssword1,\\Group3\homedirs\Lwalsh,111-1122,LWalsh@G3Zone.local
Bonnie Clive,Bonnie,Clive,Bclive,IS,ISGroup,P@ssword1,\\Group3\homedirs\Bclive,111-1123,BClive@G3Zone.local
Esther Male,Esther,Malo,Emalo,IS,ISGroup,P@ssword1,\\Group3\homedirs\Emalo,111-1124,EMalo@G3Zone.local
我得到的错误是:
Add-ADGroupMember : Missing an argument for parameter 'Identity'. Specify a par
ameter of type 'Microsoft.ActiveDirectory.Management.ADGroup' and try again.
At C:\test2.ps1:26 char:29
+ add-ADGroupMember -Identity <<<< @groups -member $_.samaccountname
+ CategoryInfo : InvalidArgument: (:) [Add-ADGroupMember], Parame
terBindingException
+ FullyQualifiedErrorId : MissingArgument,Microsoft.ActiveDirectory.Manage
ment.Commands.AddADGroupMember
另外,如果可能的话,我希望能够创建它们并将它们添加到他们自己的主目录中。