简体   繁体   English

如何使用Powershell使AD组成为另一个AD组的成员?

[英]How to make AD group member of another AD group with powershell?

i am trying to make bulk script to create groups and make some groups member of another groups (child groups). 我正在尝试制作批量脚本来创建组,并使某些组成为另一个组(子组)的成员。 i want to create groups TestGroup1,TestGroup2 (memberOf TestGroup1),TestGroup3 (memberOf TestGroup1) 我想创建组TestGroup1,TestGroup2(memberOf TestGroup1),TestGroup3(memberOf TestGroup1)

so here's my csv file that has the input groups: 所以这是我的具有输入组的csv文件:

bulk_import.csv: bulk_import.csv:

GroupName,GroupType,GroupLocation,Member

TestGroup1,Global,"OU=arSearch",
TestGroup2,Global,"OU=arSearch",TestGroup1
TestGroup3,Global,"OU=arSearch",TestGroup1

and the script to create the groups is as follows: 创建组的脚本如下:

bulk_ad_group_creation.ps1 bulk_ad_group_creation.ps1

Import-Module ActiveDirectory
#Import CSV
$csv = @()
$csv = Import-Csv -Path "C:\bulk_import.csv"

#Get Domain Base
$searchbase = Get-ADDomain | ForEach {  $_.DistinguishedName }

#Loop through all items in the CSV
ForEach ($item In $csv)
{
  #Check if the OU exists
  $check = [ADSI]::Exists("LDAP://$($item.GroupLocation),$($searchbase)")

  If ($check -eq $True)
  {
    Try
    {
      #Check if the Group already exists
      $exists = Get-ADGroup $item.GroupName
      Write-Host "Group $($item.GroupName) alread exists! Group creation skipped!"
    }
    Catch
    {
      #Create the group if it doesn't exist
      $create = New-ADGroup -Name $item.GroupName -GroupScope $item.GroupType -Path ($($item.GroupLocation)+","+$($searchbase)) -Member $item.Member
      Write-Host "Group $($item.GroupName) created!"
    }
  }
  Else
  {
    Write-Host "Target OU can't be found! Group creation skipped!"
  }
}

but it always prints that the parameter -Member cannot be found, please advise. 但是它总是打印出找不到参数-Member的信息,请告知。

i am using windows server 2008 R2. 我正在使用Windows Server 2008 R2。

i was able to solve it by changing the script as follows: 我能够通过如下更改脚本来解决它:

Import-Module ActiveDirectory
#Import CSV
$csv = @()
$csv = Import-Csv -Path "C:\bulk_import.csv"

#Get Domain Base
$searchbase = Get-ADDomain | ForEach {  $_.DistinguishedName }



#Loop through all items in the CSV
ForEach ($item In $csv)
{
  #Check if the OU exists
  $check = [ADSI]::Exists("LDAP://$($item.GroupLocation),$($searchbase)")

  If ($check -eq $True)
  {
    Try
    {
      #Check if the Group already exists
      $exists = Get-ADGroup $item.GroupName
      Write-Host "Group $($item.GroupName) alread exists! Group creation skipped!"
    }
    Catch
    {
      #Create the group if it doesn't exist
      $create = New-ADGroup -Name $item.GroupName -GroupScope $item.GroupType -Path ($($item.GroupLocation)+","+$($searchbase))
      Write-Host "Group $($item.GroupName) created!"
      if($item.MemberOf -eq ""){
         Write-Host "Group don't have parent"
         }else{
             Add-ADGroupMember -Identity $item.MemberOf -Member $item.GroupName 
           }
    }
  }
  Else
  {
    Write-Host "Target OU can't be found! Group creation skipped!"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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