【问题标题】:Powershell Script to create a new Group from a list of OU and then add User Objects from the OU to the groupPowershell 脚本从 OU 列表中创建新组,然后将 OU 中的用户对象添加到组中
【发布时间】:2012-05-04 11:02:27
【问题描述】:

我创建了一个脚本,它将查询 AD 的 OU,将它们输出到一个文件,然后将从文件中创建一个组,并对新组名稍作修改。该部分在这里起作用的是代码:

## Load Quest ActiveRoles ADManagement PSSnapin if not already loaded
if ((Get-PSSnapin -Name "Quest.ActiveRoles.ADManagement" -ErrorAction SilentlyContinue) -eq $null) { 
  Add-PsSnapin "Quest.ActiveRoles.ADManagement" } 

cd \
cls

$Type = "Security"
$Scope = "Global"
$Domain = "abc.org"


## Get Organizational Units

Get-QADObject -Type OrganizationalUnit `
| Select-Object Name, Path, DN, CanonicalName ` | Export-Csv -Path "c:\scripts\test\ou.csv" -NoTypeInformation

## Create Security Groups from Organizational Units
$Pre = "123abc"
$ou  = "ou=123abc,ou=Groups,ou=User Accounts,dc=abc,dc=org"
Import-Csv -Path "c:\scripts\test\ou.csv" | foreach {New-QADGroup -name ($Pre+$_.name) -parent $ou -sam ($Pre+$_.name)}

我遇到的问题是这个。如何从每个 OU 中获取用户对象并将它们放在单独的 csv 文件中。我有一小段代码适用于单个 OU。

$OuDomain = "OU=123abc,OU=Users,OU=User Accounts,DC=abc,DC=org"
Get-QADUser -SizeLimit 0 -searchRoot $OuDomain `
| Select-Object name, SamAccountName, UserPrincipalName `
| Export-Csv -Path "c:\scripts\test\123abc.csv" -NoTypeInformation

之后我应该能够将用户添加到新组中。

【问题讨论】:

    标签: powershell active-directory powershell-2.0


    【解决方案1】:

    首先如果您使用的是 PowerShell V2.0,您可以在 W2K8R2 上使用 Microsoft ActiveDirectory 模块。

    Add-ADGroupMember -Identity MonGroupe -Member massin
    

    第二一旦您从 OU 中获得用户,您就会对 distinguishedName 属性感兴趣。您只需将每个用户的 distinctName 分配给新组的属性 member

    我没有安装 Quest ADManagement PSSnapin,所以这里是一个简单的 ADSI 示例:

    # Starting OU
    $MonOU = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.234.200:389/ou=Monou,dc=dom,dc=fr","administrateur@dom.fr","admin")
    
    # Group creation
    $MonGroupe = $Monou.Create("Group", "cn=MonGroupe")
    $ADS_GROUP_TYPE_GLOBAL_GROUP = 0x00000002
    $ADS_GROUP_TYPE_SECURITY_ENABLED = 0x80000000
    $groupeType = $ADS_GROUP_TYPE_SECURITY_ENABLED -bor $ADS_GROUP_TYPE_GLOBAL_GROUP
    
    $MonGroupe.put("groupType",$groupeType) 
    $MonGroupe.setinfo()
    
    # Add a user to the group
    $MonGroupe.add('LDAP://cn=Marc Assin,ou=Monou,dc=dom,dc=fr')
    $MonGroupe.setinfo()
    
    # List members of a group
    $UnGroupe = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.234.200:389/cn=Mongroupe,ou=Monou,dc=dom,dc=fr","administrateur@dom.fr","admin")
    $Membres = $UnGroupe.Member
    $Membres
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 2019-05-02
      相关资源
      最近更新 更多