Powershell - 使用CSV文件将用户添加到AD组(Quest ActiveRoles管理)

时间:2014-10-15 18:28:11

标签: powershell csv active-directory quest

我有一个包含两列的CSV文件。第一列是AD组,第二列是AD用户列表(显示名称 - 不是用户ID)

CSV file snipt

有关如何将用户添加到相应AD组的任何想法。

  

e.g。将Peter Parker,Bruce Wayne,Tony Stark和Steve Rogers加入DG-GROUP1。

我需要一个脚本,重复CSV文件中的每个填充行(~2000个条目)

另外,如果使用Quest cmdlet编写它,将不胜感激。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

因此,我们需要加载CSV,在组上运行ForEach循环,并在该运行中运行每个组的用户的另一个ForEach,用分号分隔。可以做。现在,我有一个灵活的功能,它的输入我只是在这里复制/粘贴以处理查找用户帐户,因为我很懒。

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