?
Solved

Powershell variable with -SearchBase cmdlet

Posted on 2016-09-09
2
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
Dan McFadden earned 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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.
Suggested Courses

777 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