Solved

Powershell variable with -SearchBase cmdlet

Posted on 2016-09-09
2
60 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
Comment Utility
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
Comment Utility
Hi Dan,

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

Thks

JJC
0

Featured Post

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

771 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

10 Experts available now in Live!

Get 1:1 Help Now