Link to home
Start Free TrialLog in
Avatar of Jean-Jacques CELMA
Jean-Jacques CELMAFlag for France

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dan McFadden
Dan McFadden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jean-Jacques CELMA

ASKER

Hi Dan,

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

Thks

JJC