Powershell - Adding multiple users to vary groups

Hi Guys,

I am hoping one of you guru's can help. I am creating a script in powershell and i am stuck.

Scenario:

CSV (contains over 7000 rows): groupname, extensionattribute1

group1, 123456

group1, 987654

group2, 123456

group2, 987654

I was hoping there was a way that this could be scripted so that i could import the csv and add all ad users that match extensionattribute 1 to the group listed beside in the CSV.

I keep finding ways to add to a single group but not ways of adding multiple members to multiple groups.

Any help would be appreciated (with example code)

Many thanks in advance!

Kyra

September 4th, 2015 5:25pm

Sample pseudo-code:

Free Windows Admin Tool Kit Click here and download it now
September 4th, 2015 5:37pm

Thanks Bill for that, truly grateful.

With a little bit of adapting, I think the following will work (haven't tested fully yet). However, i would now like to add a bit of checking. (feel free to comment on my method of coding - i am still a beginner, all constructive criticism welcome)

Below is what i have but where the comments are is where i would like the checking to take place. I was not sure if i could get it to check the groups members before or not?

Function SyncGroups
{
Write-Verbose -Message "Syncing Groups... " -Verbose

$csv = Import-Csv -Path $filePath 

ForEach ($row in $csv){

    $CSVID = ($row.extensionAttribute1)
    $CSVGroup = ($row.GroupName)

    if (#check to see if they need adding to the group listed next to them in the csv and if they do need adding...)
        Get-ADUser -Filter {(extensionAttribute1 -eq $CSVID)} -Properties extensionAttribute1 | select SamAccountName, extensionAttribute1;
        Add-ADGroupMember $csvGroup;
        Write-Verbose -message ($row.ID + " has been added to " + $row.CSVGroup) -Verbose}
    else
    {
    # Already a member of the group listed beside in csv - do nothing
    }
}

This is just a small part of quite an extensive set of functions so all variables link back to a central config file to make changes simple :)

As always, Thanks in advance.

Kyra

September 6th, 2015 5:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics