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

Powershell variable with -SearchBase cmdlet

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
celmajj
Asked:
celmajj
1 Solution
 
Dan McFaddenSystems EngineerCommented:
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
 
celmajjAuthor Commented:
Hi Dan,

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

Thks

JJC
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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