Solved

Powershell Get-ADOrganizationalUnit command not displaying results properly.

Posted on 2014-12-05
4
768 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 83

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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.

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