2012-09-19 34 views
1

因此,以下是我如何在可信域中跨组调用递归成员的脚本。我需要帮助的是将其从查找单个组转换为多个组。powershell脚本调用要输入到当前脚本的变量列表

在线$objGrp = [ADSI]"LDAP://CN=Administrators,CN=Builtin,DC=domain,DC=com"我必须手动将其更改为我想要搜索的任何组。我反而希望这个脚本调用一个带有一个组列表的文本文件。

例如,在文本文件中会有

CN=Administrators,CN=Builtin,DC=domain,DC=com 
CN=domain admins,CN=users,DC=domain,DC=com 
CN=enterprise admins,CN=users,DC=domain,DC=com 

什么我需要添加/更改,能够做到这一点?

# Script begins 
# 
# Bind to the AD group 
$objGrp = [ADSI]"LDAP://CN=Administrators,CN=Builtin,DC=domain,DC=com" 
#[ADSI]"LDAP://CN=Administrators,CN=Builtin,DC=domain,DC=com" 
# 

$Global:GroupMembers = @() 

# Function to read the group members - nested members 
Function GetGroupMember($objGrp) 
{ 
# Enumerate the group members 
foreach($member in $objGrp.member) 
{ 
    # Bind to the each user using DN 
    $strTemp = "LDAP://" + $member 
    $objTemp = [ADSI]$strTemp 

     # Check for AD Group object based on objectCategory 
     $strCat = [System.String]$objTemp.objectCategory 
    #foreign object 

    $res = $strCat.StartsWith("CN=Foreign-Security-Principal") 

    #$strCat 
    If($res -eq $True) 
    { 

       # bind to the foreign object 
    $strTemp = "LDAP://" + $member 
    $tempObj = [ADSI]$strTemp 
    # convert binary SID to bindable string SID 
    $objBin = $tempObj.objectSID.Item(0) 
    $objSID = New-Object System.Security.Principal.SecurityIdentifier($objBin,0) 


    $srchDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://dc=domain,dc=com") 
    $dirSrchObj = New-Object System.DirectoryServices.DirectorySearcher($srchDomain) 


    #$objSID.Value 
    [System.Environment]::NewLine 
    $dirSrchObj.Filter = "((objectSID=" + $objSID.Value + "))" 
    # Search scope to sub-level 
    $dirSrchObj.SearchScope = "Subtree" 
    $dirSrchObj.PageSize = 1 
    # Array of result collection - users 
    $srchResArr = $dirSrchObj.FindOne() 

    "=======================================" 
    If ($srchResArr -ne $NULL) 
    { 
     # bind to the object 
     #$strTem = [System.String]$srchResArr.ToString() 

     $objEntry = $srchResArr.GetDirectoryEntry() 
     # read and compare the object category for group object 
     [System.String]$strTemp1 = $objEntry.objectcategory 
     $res1 = $strTemp1.StartsWith("CN=Group") 

     if($res1 -eq $True) 
     { 
      #enumerate the group members 
      Write-Host "The members of foreign group " $objEntry.Name "are: " 
      Foreach($obj in $objEntry.member) 
      { 
       $strTemp2 = "LDAP://" + $obj 
       $objTemp2 = [ADSI]$strTemp2  

       [System.String]$strTemp3 = $objTemp2.objectCategory 
       $res2 = $strTemp3.StartsWith("CN=Group") 
       if($res2 -eq $True) 
       { 
        GetGroupMember($objTemp2) 
       } 
       Else 
       { 
        $objTemp2.distinguishedName 
        $Global:GroupMembers += $objTemp2.distinguishedName 

       } 
      } 
     } 
     Else 
     { 
      "Foreign user object: " 
      $objEntry.distinguishedName 
      $Global:GroupMembers += $objEntry.distinguishedName 
     } 

    } 
    "=======================================" 
    [System.Environment]::NewLine 

    } 
    Else 
    { 


     $flag = $strCat.StartsWith("CN=Group") 


     # If it is a Group object then call this method (recursive) 
     If($flag -eq $TRUE) 
     { 
      Write-Host "++++++++++++++++++++++Recursive Call to Enumerate" $objTemp.distinguishedName 
      GetGroupMember($objTemp) 
      Write-Host "---------------------- End Recursive Call to Enumerate" $objTemp.distinguishedName 

     } 
     # If user object then display its DN 
     if($flag -eq $False) 
     { 
      $objTemp.distinguishedName 
      $Global:GroupMembers += $objTemp.distinguishedName 
      #$objTemp.sAMAccountname 
     } 
    } 
} 
} 
# 
GetGroupMember $objGrp 


"" 
"" 
"Final List:" 
$Global:GroupMembers | sort -uniq | out-file c:\temp\test.csv 

回答

1

如果你的组列表是C:\grouplist.txt那么你可以使用Get-Content通过他们得到的名称,然后循环:

$groupNames = Get-Content 'C:\grouplist.txt' 

foreach($groupName in $groupNames) 
{ 
    GetGroupMember [ADSI]$groupName 
} 

"Final List:" 
$Global:GroupMembers | sort -uniq | out-file c:\temp\test.csv