0

I have a CSV file with two columns. The first column is an AD group, the second column is a list of AD users (display name - not user id)

CSV file snipt

Any ideas on how the add the users to their corresponding AD groups.

e.g. Add Peter Parker, Bruce Wayne, Tony Stark, and Steve Rogers to DG-GROUP1.

I need a script that reiterates for every filled rows in the CSV file (~2000 entries)

Also, would be grateful if it is written using the Quest cmdlets.

Any help would be really appreciated.

Yad
  • 229
  • 1
  • 8
  • 19
  • Manually. I save the list of users into an array (There are times when the list exceeds 100) and then use a 'foreach' piped into a Add-QADGroupMember "AD Group 1" $_ . – Yad Oct 15 '14 at 18:38
  • How is your second column delimited? Or how are your columns delimited? What you have displayed is not a CSV file, that's what it would look like on screen. Kind of. – TheMadTechnician Oct 15 '14 at 20:36
  • @TheMadTechnician Sorry I just made a graphical representation at first. I edited my question to include a snip of the csv file. The second column is delimited by a semicolon (;). – Yad Oct 16 '14 at 07:51
  • Ok, that's a format that we can work with. Now, your code that you've used so far, and any attempts to automate this (even failed code, with an error) should be put into your original post. Most regulars will not answer a question if it doesn't look like you've at least tried to solve it on your own. – TheMadTechnician Oct 16 '14 at 16:01

1 Answers1

0

So, we need to load the CSV, run a ForEach loop on the groups, and within that run another ForEach on the users for each group, split by semicolon. Can do. Now, I have a function that's kind of flexible with it's input that I just copy/pasted in here to deal with looking up user accounts because I'm lazy like that.

function Get-ADAccount {
[CmdletBinding()]
  param(
    [parameter(Position = 0,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
      [string]$FirstName,
    [parameter(Position = 1,ValueFromPipelinebyPropertyName=$True)]
      [string]$LastName,
  [parameter(Position = 2,ValueFromPipelinebyPropertyName=$True)]
      [string]$Email
    )
#    Write-Host "First Name: $FirstName | Last Name: $LastName | Email: $Email"
Process{
    If($Email){$user = Get-ADUser -filter {mail -eq $Email} -Properties *}else{
    switch -Regex ($FirstName) {
        "^.+@.+\.(com|org|net|edu)" {$user = Get-ADUser -filter {mail -eq $_} -Properties *;break}
        "^.+,.+" {$LastName = $_.Split(",")[0];$FirstName = $_.Split(",")[1].TrimStart();$FirstName= $FirstName+"*";$user = Get-ADUser -Filter {GivenName -like $FirstName -and Surname -eq $LastName} -Properties *;break}
        ".+ .+" {$LastName = $_.substring($_.IndexOf(" ")+1,$_.Length-$_.IndexOf(" ")-1);$FirstName = $_.Split(" ")[0];$FirstName= $FirstName+"*";$user = Get-ADUser -Filter {GivenName -like $FirstName -and Surname -eq $LastName} -Properties *;break}
        ".+" {$FirstName= $FirstName+"*";$user = Get-ADUser -Filter {GivenName -like $FirstName -and Surname -eq $LastName} -Properties *}
    }}
#    if(!($user.name -gt "")){Write-Host "Searching 'GivenName -like $FirstName -and Surname -eq $LastName'";$user = Get-ADUser -Filter {GivenName -like $FirstName -and Surname -eq $LastName} -Properties *}
    if(!($user.name -gt "")){$user = "User not found."}else{foreach($phone in $user){if($phone.mobilephone -gt ""){$phone.mobilephone = "{0:(###) ###-####}" -f [int64]$($phone.mobilephone -replace "[^\d]").TrimStart("1");$phone.mobile= $phone.mobilephone}}}
    $user
}
}
$GroupsToUpdate = Import-CSV C:\Path\To\File.csv
ForEach($Group in $GroupsToUpdate){
    $Group.ApplicationApprovers.Split(";") | Get-ADAccount | ForEach{$Group.'New DG Group' | Add-QADGroupMember -Member $_.DistinguishedName}
}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56