Search code examples
powershellcsvansibleactive-directorygroup

List of Active Directory groups not showing from PowerShell code


I input a list of groups for each user in CSV, and tried to create users using PowerShell code.

This is the PowerShell code:

  - name: Change group for AD users
    ansible.windows.win_powershell: 
      script: |     
        [CmdletBinding()]
        param (
            [array]
            $datalist
        )

        $output = foreach ($user in $datalist) {
          $name = $user.SamAccountName
          $groups = $user.Groups
          $users = Get-ADUser -Filter "SamAccountName -eq '$name'" 
          Get-ADUser -Filter "SamAccountName -eq '$name'" -Properties MemberOf | ForEach-Object {$_.MemberOf | Remove-ADGroupMember -Members $users -Confirm:$false}
          Add-ADGroupMember -Identity $groups -Members $users
        }

      parameters:
        datalist: "{{ hostvars.localhost.list }}"

I ended up getting this error:

"message": "Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported."

Also tried '$groups':

"message": "Cannot find an object with identity: '$groups' under: 'DC=adexample,DC=local'.",

And "$groups":

"message": "Cannot find an object with identity: 'CN=GroupA,OU=Groups,DC=adexample,DC=local CN=Test Group,OU=Groups,DC=adexample,DC=local' under: 'DC=adexample,DC=local'.",

This is how I input my list of groups into the CSV file:

Groups
CN=GroupA,OU=Groups,DC=adexample,DC=local;CN=Test Group,OU=Groups,DC=adexample,DC=local
CN=GroupA,OU=Groups,DC=adexample,DC=local;CN=Test Group,OU=Groups,DC=adexample,DC=local

What is the right way to write $groups so that my list of groups can be output correctly?


Updated with CSV in plain text:

FirstName,SamAccountName,path,UserPrincipalName,Groups

Greg,gre.b87,"OU=Temporary Users,DC=adexample,DC=local",[email protected],"CN=GroupA,OU=Groups,DC=adexample,DC=local;CN=Test Group,OU=Groups,DC=adexample,DC=local"

Zee,zeef.cd,"OU=Temporary Users,DC=adexample,DC=local",[email protected],"CN=GroupA,OU=Groups,DC=adexample,DC=local;CN=Test Group,OU=Groups,DC=adexample,DC=local"


I adapted bhuvanachand komara's to mine and it worked for me:

    $output = foreach ($user in $datalist) {
        $name = $user.SamAccountName
        $groups = $user.Groups -split ";"
        Get-ADUser -Filter "SamAccountName -eq '$samname'" 
        $users = Get-ADUser -Filter "SamAccountName -eq '$samname'" 
          foreach ($group in $groups) {
            Add-ADGroupMember -Identity $group -Members $user
          }
        Get-ADUser -Filter "SamAccountName -eq '$samname'" -Properties MemberOf | ForEach-Object {$_.MemberOf | Remove-ADGroupMember -Members $users -Confirm:$false}
     }

The main thing is that I need to add $groups = $user.Groups -split ";" and another foreach loop for the groups.


Solution

  • $csvFile = 'path\to\csv\file.csv'
    
    $users = Import-Csv -Path $csvFile
    
    foreach ($user in $users) {
        $samAccountName = $user.SamAccountName
        $givenName = $user.GivenName
        $surname = $user.Surname
        $password = $user.Password
        $email = $user.Email
        $groups = $user.Groups -split ","
    
        $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
        New-ADUser -SamAccountName $samAccountName -GivenName $givenName -Surname $surname -DisplayName "$givenName $surname" -EmailAddress $email -AccountPassword $securePassword -Enabled $true
    
        foreach ($group in $groups) {
            Add-ADGroupMember -Identity $group -Members $samAccountName
        }
    }
    

    Example CSV

    SamAccountName,GivenName,Surname,Password,Email,Groups user1,chand,komara,Password1,[email protected],group1,group2 user2,bhuvan,unnava,Password2,[email protected],group2,group3

    For each user in the $users array, the code creates a new Active Directory user using the New-ADUser cmdlet with the specified SamAccountName, GivenName, Surname, DisplayName, EmailAddress, and account password. It then adds the user to the specified groups using the Add-ADGroupMember cmdlet.