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
Thanks in advance!