Search for partial match in array

Hey!

Im trying to loop through an list of servers, an match if they resides in an array of Active Directory Organizational Units.
But im having some troubles with this, without doing an foreach inside my foreach object - which i would like to avoid.

The snippet is like this:

$ExcludedOUs = @(
    "OU=salary,OU=country1,OU=Servers,OU=Administration,DC=domain,DC=com"
    "OU=salary,OU=country2,OU=Servers,OU=Administration,DC=domain,DC=com"
    "OU=salary,OU=country3,OU=Servers,OU=Administration,DC=domain,DC=com"
    "OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com"
)
foreach ($WinServer in $WinServers) {
    #Confirm that the server actually exist in the AD, since the $WinServers list comes from multiple ADs
    $DistinguishedName = $(Get-ADComputer -Filter "Name -eq '$WinServer'").DistinguishedName
    IF ($DistinguishedName) {
        if ($DistinguishedName -in $ExcludedOUs) {
            $WinServer
        }
    }
}

The DistinguishedName for the servers are always longer than the OU, and can have several sub OUs under each of them - an example of an server DN could be:
“CN=Server1,OU=Navision,OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

“CN=Server1,OU=2017,OU=Navision,OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

I need to match if the DN is an exact match for the substring, since i would like to have the servers under this OU included:
OU=HR,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

OU=Compliance,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

I hope this makes sense, and that you can help me out of this struggle, before i go bald :slight_smile:
Thanks in advance!

Why that? What’s wrong with a nested loop if it does the job?

I’m not sure. Your explanation with the examples does not fit to the code you posted. Are you just looking for a particular part of the DN of the servers?

Hi Olaf,

Thanks for the fast reply.

In my head, an foreach inside an foreach is not the best solution - might be my OCD kicking in :slight_smile:

Im trying to exclude all servers, that are in one of those “base OUs”.
So i would like to find these servers:
“CN=Server1,OU=Navision,OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”
“CN=Server2,OU=2017,OU=Navision,OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”
“CN=Server3,OU=2019,OU=Navision,OU=salary,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

But still include the servers, that is in these OUs:
OU=HR ,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

OU=Compliance ,OU=country4,OU=Servers,OU=Administration,DC=domain,DC=com”

So i need the server substring DN to match against the $ExcludedOUs

Please, when you post code or sample data format it as code.
Thanks in advance.

I still cannot see any problem with that. Why don’t you try it at least? !!! :wink:

If it’s just that you could “outsource” the check to a small helper function. This way it wouldn’t look like a nested loop even if it is. :wink:

1 Like