简体   繁体   中英

Powershell - Using CSV file to add Users to AD Group (Quest ActiveRoles Management)

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文件摘要

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

eg 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.

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}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM