无法在foreach循环中使用Get-ADGroup

时间:2019-03-22 08:20:27

标签: arrays powershell loops foreach active-directory

我想在嵌套的Get-ADGroup循环中使用foreach命令。但是以某种方式,该命令没有任何结果。该命令和过滤器是corect,就像您在循环后的最底部看到的那样,无论出于何种原因,同一条指令都可以在其中完美地运行。

$file = "\\path"
$data = Import-Csv $file -Delimiter ";" -Encoding UTF7 |
        select -First 5

Measure-Command {
    foreach ($item in $data ) {
        $tiefe = $($item.'Tiefe')
        $pfad = $($item.'Pfad')
        $recht = $($item.'Recht')
        $trustee = $($item.'trustee')

        $LDAPDirectoryService = 'IP-Adresss'
        $DomainDN = 'o=enterprise'

        $LDAPFilter = "cn=$trustee"

        $null = [System.Reflection.Assembly]::LoadWithPartialName('System.DirectoryServices.Protocols')
        $null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')

        $LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
        $LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Anonymous
        $LDAPServer.SessionOptions.ProtocolVersion = 3
        $LDAPServer.SessionOptions.SecureSocketLayer = $false

        $Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
        $AttributeList = @('*')

        $SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $DomainDN,$LDAPFilter,$Scope,$AttributeList

        $groups = $LDAPServer.SendRequest($SearchRequest)
        $groups

        #Prüft ob Gruppe existiert
        if ($groups.Entries.Count -eq 0) {
            Write-Host " Group not found!" `n -Foregroundcolor Red $LDAPFilter
            #Speichert alle nicht gefundenen Gruppen zur manuellen Nachbearbeitung
            Add-Content -Path \\Path -Value "$LDAPFilter"
        }

        foreach ($group in $groups.Entries) {
            #Listet alle Member der oben übergebenen Gruppe auf
            $users = $group.Attributes['member'].GetValues('string')

            foreach ($user in $users) {
                Write-Host $user
                #Hier den User zur AD Gruppe hinzufügen
                Write-Host "user zur Gruppe hinzufügen $pfad-$recht" -ForegroundColor Red

                # This little Boy doesnt work
                Get-ADGroup -Properties Name, Description -Filter 'Name -like "F-KT-*"' |
                    where {$_.Description -like "*$pfad" -and $_.Name.EndsWith($recht)}

                #Add-ADGroupMember -Identity S-1-5-21-219376080-2991882224-574971396-34759 -Members $user -Whatif
            }
        }
    }
}

# Here the command works without a fault
Get-ADGroup -Properties Name, Description -Filter 'Name -like "F-KT-*"' |
    where {$_.Description -like "*$pfad" -and $_.Name.EndsWith($recht)}

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么,但是我找到了适合我的解决方案。

$AD = Get-ADGroup -Properties Name, Description -Filter 'Name -like "F-KT-*"' | where {$_.Description -like "*$pfad" -and $_.Name.endswith($recht) }
write-Host $AD.Name -ForegroundColor Yellow

也许有人可以告诉我为什么我必须在循环中将get-ADGroup声明为变量,但是在没有它的情况下无法工作。