Get-adcomputer script, I'm missing something

$OU = Get-content 'C:\Powershell Projects\computerOU\OU.txt' |
ForEach-Object {Get-adcomputer -filter {(enabled -eq $True)} -SearchBase $OU} | Export-Csv 'C:\Powershell Projects\ComputerOU\results.csv'

Open in new window


Error

Get-ADComputer : Cannot validate argument on parameter 'SearchBase'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:2 char:74
+ ... Object {Get-adcomputer -filter {(enabled -eq $True)} -SearchBase $OU}
+                                                                      ~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADComputer


Why doesn't that work?

If I wrap the $OU variable in "$OU" it gives me

Get-adcomputer : An empty SearchBase is only supported while connected to a GlobalCatalog.
At line:2 char:17
+ ... ach-Object {Get-adcomputer -filter {(enabled -eq $True)} -SearchBase  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ArgumentException
    + FullyQualifiedErrorId : An empty SearchBase is only supported while connected to a GlobalCatalog.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
LVL 18
Alex GreenProject Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
$OU would receive the results of the pipeline; line 1 is not a simple statement, it opened a pipeline (note the pipe symbol at the end).
Get-Content 'C:\Powershell Projects\computerOU\OU.txt' |
	ForEach-Object {Get-ADComputer -Filter {(enabled -eq $True)} -SearchBase $_} |
	Export-Csv 'C:\Powershell Projects\ComputerOU\results.csv'

Open in new window

One of the reasons why proper indentation is so helpful ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex GreenProject Systems EngineerAuthor Commented:
URRRGHHHHHH

Thank you :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.