Solved

Powershell variable with -SearchBase cmdlet

Posted on 2016-09-09
2
85 Views
Last Modified: 2016-09-09
I have created a powershell that list all computers in a specific OU. Here is the script :

Import-Module activedirectory

[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
$Continent = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Continent (Europe, Asia, North America....)", "Input Box")

IF(!$Continent)
   
    {
       
    #Continent does not Exist in AD

    [System.Windows.Forms.MessageBox]::Show("Continent"+ $Continent +" does not exist in AD. Please check in AD how it is writing", "Status")
   
    }
   
    Else

        {

    $Country = [Microsoft.VisualBasic.Interaction]::InputBox("Enter country Name (France, Italy, Holland, Austria....)", "Input Box")
         
        IF(!$Country)

        {

        #Country does not exist in AD

        [System.Windows.Forms.MessageBox]::Show("Country"+ $Country +" does not exist in AD. Please check in AD how it is writing", "Status")

        }

        Else
       
        {

        $Location = [Microsoft.VisualBasic.Interaction]::InputBox("Location Name (Montrouge, Biot, Iwuy, Milan....)", "Input Box")
       
            IF(!$Location)

        {

        #Location does not exist in AD

        [System.Windows.Forms.MessageBox]::Show("Location" + $Country + " does not exist in AD. Please check in AD how it is writing", "Status")

        }

        Else
       
        {    

        $OU = "OU=Desktops,OU=Computers,OU=" + $Location + ",OU=" + $Country + ",OU=" + $Continent + ",OU=Root,DC=MyDomain,DC=lan"

Get-ADComputer -Filter * -SearchBase "$OU" -Properties conairSerial, OperatingSystem | select-object Name, OperatingSystem, ConairSerial | FT -AutoSize | Export-Csv C:\trash\france.txt

        }
        }
    }

When I execute that script I am getting the error :

Get-ADComputer : Object not found in Active Directory
Au caractère C:\trash\SearchComputers.ps1:56 : 1
+ Get-ADComputer -Filter * -SearchBase "$OU" -Properties conairSerial, OperatingSy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-ADComputer], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Objet de l’annuaire non trouvé,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Any idea how I can fix it?

Thks

JJC
0
Comment
Question by:celmajj
2 Comments
 
LVL 26

Accepted Solution

by:
Dan McFadden earned 500 total points
ID: 41791042
The error appears to indicate that the OU you are attempting to search in, does not exist.

Can you verify that the string being passed to the SearchBase is:

1. properly formatted
2. the referenced OU exists

Also, try building the $OU like this:

"OU=Desktops,OU=Computers,OU=$Location,OU=$Country,OU=$Continent,OU=Root,DC=MyDomain,DC=lan"

Open in new window


When you run the Get-ADComputer command manually, what is the result?

Dan
0
 

Author Comment

by:celmajj
ID: 41791311
Hi Dan,

your suggestion with "  try building the $OU like this:" works.

Thks

JJC
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now