Thanks for the responses - looks like I'm doing quite a bit wrong. Here's where I stand, and since I don't have a full list of DNs for the OUs I'm trying to get results on - I'm trying to simply use the OU name in list.txt.
Get-Content c:\list.txt |
ForEach {
Get-ADOrganizationalUnit $_ -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | Select-Object Name,state
} |
Export-Csv c:\temp\output.csv -NoTypeInformation
With the &_ element in there I get a positional parameter error, but without it I get results from all OUs under the one specified in the script.
I assume you mean "$_", not "&_". Yes, it doesn't know what you mean by just putting the "$_" there; you already have "-Filter *", so it looks like you're trying to use $_ as an additional filter but there are
no positional parameters that fit so the cmdlet complains about it.
Regardless, I think what you'll need to do, since you say you only have the names of OUs, not DNs or GUIDs, is to use something like this to find the OUs you're looking for (note: not tested code!):
Get-ADOrganizationalUnit -Filter * -SearchBase "OU=xx,DC=xx,DC=xx,DC=xx" -SearchScope Subtree | ? { $_.DistinguishedName -match "^OU=$($_),.*" } | Select-Object Name,State
This uses the Where-Object cmdlet to filter the list of OUs returned to ones that match the names in the text file. Note, however, that since OU names are not unique you may end up with more than one that matches a single input name.
-
Edited by
D'Thompson
Monday, August 31, 2015 5:15 PM