• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19
  • Last Modified:

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
0
Alex Green
Asked:
Alex Green
1 Solution
 
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
 
Alex Green3rd Line Server SupportAuthor Commented:
URRRGHHHHHH

Thank you :D
0

Join & Write a Comment

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now