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
Powershell 具有多个ForEach对象的x次递归循环_Powershell_Recursion_Active Directory - Fatal编程技术网

Powershell 具有多个ForEach对象的x次递归循环

Powershell 具有多个ForEach对象的x次递归循环,powershell,recursion,active-directory,Powershell,Recursion,Active Directory,我一直在使用该准则,以期对其进行修改,使其仅向下查看X级直接下属(例如,首席执行官加上2级) 我正在使用一个额外的$count变量,并决定使用if($count-ge0){}和$count--来控制递归,但是我一直在上下代码,在每个For循环中,尝试找到正确的位置;结果喜忧参半,有时还很搞笑 当我盯着看的时候,我很确定把if语句放在getaddirectreports-SamAccountName$周围需要2分钟的时间。我花了大约6个多小时来让它工作。有人准备接受挑战吗 function Get

我一直在使用该准则,以期对其进行修改,使其仅向下查看X级直接下属(例如,首席执行官加上2级)

我正在使用一个额外的
$count
变量,并决定使用
if($count-ge0){
}
$count--
来控制递归,但是我一直在上下代码,在每个For循环中,尝试找到正确的位置;结果喜忧参半,有时还很搞笑


当我盯着看的时候,我很确定把
if
语句放在
getaddirectreports-SamAccountName$
周围需要2分钟的时间。我花了大约6个多小时来让它工作。有人准备接受挑战吗

function Get-ADdirectReports {
    Param($SamAccountName, $count)

    Get-Aduser -Identity $SamAccountName -Properties directreports -Server contoso.net:3268 | ForEach-Object {     
        ($_.directreports) | ForEach-Object {
            # Output the current Object information
            Get-ADUser -Identity $_ -Properties manager -Server contoso.net:3268 |
                Select-Object -Property Name, SamAccountName, @{L="Manager";E={
                    (Get-Aduser -Identity $_.Manager -Server contoso.net:3268).SamAccountName
                }}
            # Find the DirectReports of the current item
            Get-ADdirectReports -SamAccountName $_
        }
    }
}

Get-ADdirectReports TheCEO 5

以下代码给出了所需的结果:

function Get-ADdirectReports
{
PARAM ($SamAccountName, $count)

    Get-Aduser -identity $SamAccountName -Properties directreports -server contoso.net:3268 | ForEach-Object {
        $tick++
        ($_.directreports) | foreach-object {
           # Output the current Object information, uses ad-object just in case a contact is used
            Get-ADobject -identity $_ -Properties manager,SamAccountName -server contoso.net:3268 | Select-Object -Property Name, SamAccountName, DistinguishedName, @{ L = "Manager"; E = { (Get-Aduser -identity $_.manager -server contoso.net:3268).samaccountname }}, @{ L = "Level"; E = {$tick} }
            if ($tick -le $count) {
                # Find the DirectReports of the current item
                Get-ADdirectReports -SamAccountName $_
            }
        }
    }
}
Get-ADdirectReports carters 2

我回答的不多,但这个很有趣

“#查找当前项的DirectReports”只有samaccountname,没有计数;因此,计数默认为0:)

尝试:


Get ADdirectReports-SamAccountName$\
->
if($count-gt 0){Get ADdirectReports-SamAccountName$\$u($count-1)}
?这就是我的想法,我在第三段中提到过。不管$count值是多少,它只返回低于
$SamAccountName
 # Find the DirectReports of the current item 
Get-ADdirectReports -SamAccountName $_ $count