Can I use a variable in my seach base?

2023-02-03T00:06:22.77+00:00

Afternoon all,

I'm working on a script that will allow folks to generate a list of computer names from an OU they input. The last bit is having an issue with my search base input and I'm trying to figure out what I'm doing wrong. Here is the code:

$computers = get-content C:\Users\$env:username\Desktop\computers.csv
$search=Get-ADOrganizationalUnit -filter 'name -like "$ou"' | select DistinguishedName

$ou=read-host -prompt 'Please enter OU your computers reside in and we will create that for you'

Write-host "You have chosen $ou, processing now"

#Generate the list of computers
Get-ADComputer -Filter * -SearchBase "$search" | select name | out-file c:\users\$env:usersname\desktop\computers.csv

Can someone tell me why it won't take that in the search base? Your assistance will be appreciated.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,583 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,811 Reputation points
    2023-02-03T03:14:37.4866667+00:00

    There are a few reasons why this doesn't work:

    • You run Get-ADOrganizationalUnit before populating the variable "ou"
    • You use Select-Object to populate the $search variable. This places a PSCustomObject into the $search variable, not the distinguishedname as you thought it would. To use the name of the OU in the -SearchBase parameter you'd have to use it in this way: $search.distinguishetname

    There is another problem, too. You don't check to see if the Get-ADOrganizationalUnit actually found the OU, or if there were multiple OUs with similar names.

    I think this is closer to what you intended:

    $ou=read-host -prompt 'Please enter OU your computers reside in and we will create that for you'
    Write-host "You have chosen $ou, processing now"
    $search=Get-ADOrganizationalUnit -filter 'name -like "$ou"' | select DistinguishedName  # creates a PSCustomObject
    
    $search = (Get-ADOrganizationalUnit -filter 'name -like "$ou"').DistinguishedName
    if ($null -eq $search -OR $search -is [array]){
        Write-Host "Either $ou was not found or multiple OUs were found"
        Write-Host "Please try again"
        return
    }
    
    #Generate the list of computers
    Get-ADComputer -Filter * -SearchBase $search |
        Select-Object Name |
            Export-CSV c:\users\$env:usersname\desktop\computers.csv
    
    

    The $computers variable is never referenced in your code so I left it out of the answer. Also, if the file "computers.csv" is really a CSV it'll have a "header" as the first line and it will be part of the data. Did you mean to use Import-CSV where you've used Get-Content?


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,811 Reputation points
    2023-02-03T03:37:07.07+00:00

    Duplicate answer because the previous answer didn't appear after 15 minutes. :-(

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.