[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell Get-ADOrganizationalUnit command not displaying results properly.

Posted on 2014-12-05
4
Medium Priority
?
857 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

656 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