【问题标题】:Can't use Get-ADGroup in foreach loop不能在 foreach 循环中使用 Get-ADGroup
【发布时间】:2019-08-13 04:43:22
【问题描述】:

我想在嵌套的foreach 循环中使用Get-ADGroup 命令。但不知何故,该命令没有任何结果。命令和过滤器是正确的,正如您在循环后的最底部看到的那样,无论出于何种原因,相同的指令都可以完美地工作。

$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)}

【问题讨论】:

  • 我不确定为什么一个工作而不是另一个工作,但我对你的问题是,你为什么需要它?您已经可以访问一个组,因为您在 foreach 循环中。您应该能够运行Add-ADGroupMember -Identity $group -Members $user。此外,您是使用完整的 Active Directory 域还是 LDS 执行此操作?不过,我可能会从您的帖子中遗漏一两件事。
  • 有点棘手,我尝试将安全组从 LDS 迁移到 AD。
  • 所以 $group 值是 LDS 组名称,但现在我需要 AD 名称来添加这些用户。

标签: arrays powershell loops foreach active-directory


【解决方案1】:

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

$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 声明为循环中的变量,但在没有它的情况下它的工作之外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2015-07-15
    相关资源
    最近更新 更多