Solved

Powershell Get-ADOrganizationalUnit command not displaying results properly.

Posted on 2014-12-05
4
754 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
  • 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 82

Accepted Solution

by:
oBdA earned 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now