如何在新用户脚本中将多个用户添加到多个组?

时间:2018-03-24 16:04:51

标签: powershell active-directory powershell-v2.0

我有一个正在制作活动目录用户的脚本,并且它运行良好。

这里有一件事,我需要这些用户在创建后自己添加它们

某些团体。

所以我发现这是一个小范围Add-ADPrincipalGroupMembership

但是我不知道如何将这个CmdLet组合到我的脚本中(我在PowerShell中更多地了解了一个月)

我试图使用另一个foreach声明,但它没有用

以下是代码:

cls
#get the csv file
$filepath = import-csv "C:\users.csv"

#set the variable for the uers
$newusers = $filepath

#set Passwords for new users 
$securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force

#start the loop 
foreach ($user in $newusers) {

    #get user information
    $firstname = $user.'First Name'.Trim()
    $lastname = $user.'Last Name'.Trim()
    $loginname= $user.SamAccountName
    $UsrPrincipalName = $user.UserPrincipalName
    $jobtitle = $user.'Job Title'
    $Department= $user.Department
    $Description = $user.Description
    $OuPath= $user.Path
    $LoginScript=$user.ScriptPath
    $displayname= $user.DisplayName

    #create the users in active directory
    $vars = @{
        Name = "$firstname $lastname"
        GivenName = $firstname
        Surname = $lastname
        UserPrincipalName = $UsrPrincipalName
        SamAccountName = $loginname
        Path = $OuPath
        ScriptPath = $LoginScript
        AccountPassword = $securepassword
        ChangePasswordAtLogon = $false
        Department = $Department
        DisplayName = $displayname
        Description = $Description
        Title = $jobtitle
        Enabled = $true
    }

    #Editors comment: Make a hashtable and use splatting when specifying lots of parameters
    $newcreatedusers = New-ADUser @vars -PassThru

    #starting a loop for adding the users to the groups
    Write-Host "`n"
    Write-Host "The account for $firstname $lastname created in $OuPath successfully"

}


$filepath = $Adgroups

foreach ($group in $Adgroups){

    $adgroup = $group.Groups.splite(',')

    Add-ADPrincipalGroupMembership -Identity $group.Groups -members $SamAccountName

}

CSV文件: CSV Sample

1 个答案:

答案 0 :(得分:0)

经过漫长的“游戏”后,这是创建新用户并将其从CSV文件添加到多个组的代码:

cls
#get the csv file
$filepath = import-csv "C:\users.csv"

#set the variable for the uers
$newusers = $filepath

#set Passwords for new users 

$securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force

#start the loop for adding users

foreach ($user in $newusers) {


#Get user information

$firstname = $user.'First Name'.Trim()
$lastname = $user.'Last Name'.Trim()

#The "SamAccountName" is for the Pre windows 2000 login name has to be less than 20 characters
$loginname= $user.SamAccountName

#The "UserPrincipalname" is the regular login username
$UsrPrincipalName = $user.UserPrincipalName

$jobtitle = $user.'Job Title'
$Department= $user.Department
$Description = $user.Description
$OuPath= $user.Path
$LoginScript=$user.ScriptPath
$displayname= $user.DisplayName

#Get Groups information
$group1 = $user.Group1
$group2 = $user.Group2
$group3 = $user.Group3
$group4 = $user.Group4    

#Creat the users in active directory

New-ADUser -Name "$firstname $lastname" -GivenName $firstname `
 `
 -Surname $lastname  -UserPrincipalName $UsrPrincipalName `
 `
 -SamAccountName $loginname -Path $OuPath -ScriptPath $LoginScript  `
 `
 -AccountPassword $securepassword -ChangePasswordAtLogon $false  `
 `
 -Department $Department -DisplayName $displayname `
 `
 -Description $Description -Title $jobtitle  -Enabled $true  



 #Add the users in to Groups

 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group1
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group2
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group3
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group4



Write-Host "`n"
Write-Host "The account for $firstname $lastname created in $OuPath successfully"

}