?
Solved

Powershell Get-ADOrganizationalUnit command not displaying results properly.

Posted on 2014-12-05
4
Medium Priority
?
835 Views
Last Modified: 2014-12-05
I'm trying to run a simple Powershell script that gets the Active Directory OUs. The script is getting the results I want but it's not displaying them properly.  It's displaying  as "Object[ ] Array" Instead of the OU name (Also see screenshot).

Now if I output the results to a text file.. everything displays properly in the text file. Is there something I'm doing wrong here?

My code is below.
	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	foreach ($items in $ADResults)
	{ $ADStartOU.Items.Add($ADResults) }

Open in new window

powershellresults.png
0
Comment
Question by:AnthonySmithMCP
[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
4 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40482997
Guess you are using powershell forms and you have not provided the entire code how you are feeding the OUs to the drop down box. Please post the entire code.
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 40483034
Main error: in the ForEach loop, you're adding the complete $ADResults array to $ADStartOU, instead of the loop item.
Then it's not clear where and how "$ADStartOU" is defined.
Finally, which OU property you actually want in the output; I'm guessing the DN?
Try
$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
ForEach ($OU In $ADResults) {
	"Found OU: '$($OU.DistinguishedName)'" | Write-Host
	$ADStartOU.Items.Add($OU.DistinguishedName)
}

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40483095
Thank you oBdA,

I changed it to the code below. However the results is  giving the full distinguished name.. All i want is the name of the OU. is there anyway to filter it?

	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(Name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	foreach ($items in $ADResults)
	{ $ADStartOU.Items.Add($items) }

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40483110
Got it working with the code below. Thank you soooo much
	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	ForEach ($Items In $ADResults)
	{
		$ADStartOU.Items.Add($items.Name)
	}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

770 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