Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell递归检索组成员_Powershell_Active Directory_Powershell 3.0 - Fatal编程技术网

powershell递归检索组成员

powershell递归检索组成员,powershell,active-directory,powershell-3.0,Powershell,Active Directory,Powershell 3.0,我有一个相当大的审计项目,我希望自动化 我需要得到每个用户的名字,SamAccountName,标题和部门谁是一个组的一部分。问题是,组中有组,而这些组中也有组。另一个问题是,大约99%的组在其显示名称中有一个星号(而不是SamAccountName) 这是我目前拥有的代码,它工作正常,直到它收到一个名称中带有星号的组。。(因此出现了.Replace(“*”,”)部分……有人知道如何解决这个问题吗 function Get-NestedGroupMember { [CmdletBinding()

我有一个相当大的审计项目,我希望自动化

我需要得到每个用户的名字,SamAccountName,标题和部门谁是一个组的一部分。问题是,组中有组,而这些组中也有组。另一个问题是,大约99%的组在其显示名称中有一个星号(而不是SamAccountName)

这是我目前拥有的代码,它工作正常,直到它收到一个名称中带有星号的组。。(因此出现了.Replace(“*”,”)部分……有人知道如何解决这个问题吗

function Get-NestedGroupMember {
[CmdletBinding()] 
param(
    [Parameter(Mandatory)] 
    [string]$Group 
)
$broke = @();
## Find all members  in the group specified 
$members = Get-ADGroupMember -Identity $Group 
foreach ($member in $members){
    ## If any member in  that group is another group just call this function again 
    if ($member.objectClass -eq 'group'){
        $memberGroup = $($member.Name).Replace("*", "")
        try{
            Get-NestedGroupMember -Group "$($memberGroup)"
        }catch{
            $broke += "$($memberGroup)`n"
        }
    }else{
        ## otherwise, just  output the non-group object (probably a user account)
        $member.Name  
    }
}
Write-Host "`nThe following groups could not be found automatically.`n`n$($broke)"
}

$getGroup = Read-Host -Prompt "Group name"

Get-NestedGroupMember $getGroup

正如Bill_Stewart所说,您正在尝试实现已经存在的功能。它有一个-Recursive参数,可以搜索所有嵌套组并为您获取成员。然后,您可以通过管道将输出发送到select,并仅获取您关心的属性

AD:
  TopGroup
    Betty
    Bob
    NestedGroup1
      Joe
      Frank
    NestedGroup2
      George
      Herman
使用-递归

Get-AdGroupMember TopGroup -Recursive | Select-Object SamAccountName
Betty
Bob
Joe
Frank
George
Herman

您没有使用
-Recursive
参数,因为。。。