today i got a maybe simple problem. I want to use a get-ADGroup command in nested forEach-Loops. But somehow the command results nothing back. The command and Filters are corect, as you can see it at the very bottom after the Loops, the same instruction works there perfectly, for whatever reason.
[pre]
$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’)
Write-Host “zur Kontrolle: $trustee” -ForegroundColor Green
$LDAPDirectoryService = ‘XX_Ip-Adress_XX’
$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’)
$AD_Group = get-ADGroup -Properties Name, Description -Filter 'Name -like “F-KT-"’ | where {$_.Description -like "$pfad” -and $_.Name.endswith($recht) }
$AD_Group
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
#Get-ADGroup -Properties Name, Description -Filter ‘Name -like “F-KT-"’ #| where {$.Description -eq “I:\DATA-DE\KT$pfad” -and $.Name.endswith($recht) }
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
}
}#for-Each
}#Measure-command
Get-ADGroup -Properties Name, Description -Filter 'Name -like “F-KT-"’ | where {$_.Description -like "$pfad” -and $_.Name.endswith($recht) }
[/pre]
Here, the command works perfectly fine, for whatever reason