Jean-Jacques CELMA
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.A ssembly]:: LoadWithPa rtialName( “Microsoft.VisualBasic”)
$Continent = [Microsoft.VisualBasic.Int eraction]: :InputBox( "Enter Continent (Europe, Asia, North America....)", "Input Box")
IF(!$Continent)
{
#Continent does not Exist in AD
[System.Windows.Forms.Mess ageBox]::S how("Conti nent"+ $Continent +" does not exist in AD. Please check in AD how it is writing", "Status")
}
Else
{
$Country = [Microsoft.VisualBasic.Int eraction]: :InputBox( "Enter country Name (France, Italy, Holland, Austria....)", "Input Box")
IF(!$Country)
{
#Country does not exist in AD
[System.Windows.Forms.Mess ageBox]::S how("Count ry"+ $Country +" does not exist in AD. Please check in AD how it is writing", "Status")
}
Else
{
$Location = [Microsoft.VisualBasic.Int eraction]: :InputBox( "Location Name (Montrouge, Biot, Iwuy, Milan....)", "Input Box")
IF(!$Location)
{
#Location does not exist in AD
[System.Windows.Forms.Mess ageBox]::S how("Locat ion" + $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=l an"
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.p s1:56 : 1
+ Get-ADComputer -Filter * -SearchBase "$OU" -Properties conairSerial, OperatingSy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-ADComputer], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : Objet de l’annuaire non trouvé,Microsoft.ActiveDir ectory.Man agement.Co mmands.Get ADComputer
Any idea how I can fix it?
Thks
JJC
Import-Module activedirectory
[void][System.Reflection.A
$Continent = [Microsoft.VisualBasic.Int
IF(!$Continent)
{
#Continent does not Exist in AD
[System.Windows.Forms.Mess
}
Else
{
$Country = [Microsoft.VisualBasic.Int
IF(!$Country)
{
#Country does not exist in AD
[System.Windows.Forms.Mess
}
Else
{
$Location = [Microsoft.VisualBasic.Int
IF(!$Location)
{
#Location does not exist in AD
[System.Windows.Forms.Mess
}
Else
{
$OU = "OU=Desktops,OU=Computers,
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.p
+ Get-ADComputer -Filter * -SearchBase "$OU" -Properties conairSerial, OperatingSy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-ADComputer], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : Objet de l’annuaire non trouvé,Microsoft.ActiveDir
Any idea how I can fix it?
Thks
JJC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
your suggestion with " try building the $OU like this:" works.
Thks
JJC