[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Export LDAP information to CSV with powershell

Posted on 2012-09-14
2
Medium Priority
?
2,704 Views
Last Modified: 2012-09-14
Hello,

I'm trying to create a little script to export some information of my ldap to a CSV file.

I'm using this little script :
$strFilter = "((objectCategory=User))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Utilisateurs,DC=myldap")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 10
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name","postofficebox"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

$myList = @()
foreach ($objResult in $colResults) {
    $objItem = $objResult.Properties
    $myUSer = "" | Select-Object Name, PostOfficeBox
    $myUser.Name = $objResult.Properties["name"]
    $myUser.PostOfficeBox = $objResult.Properties["postofficebox"]
    $myList += $myUser
}
    

Open in new window


but when I export $mylist to csv, I get only the object type and not the value as you can see :
#TYPE Selected.System.String
"Name","PostOfficeBox"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"

Open in new window


I running it for a quite long time and I can get out where I'm wrong.
Any help should be very welcome.
0
Comment
Question by:Sybux
2 Comments
 
LVL 37

Accepted Solution

by:
Jamie McKillop earned 1000 total points
ID: 38399808
Hello,

Download the free Quest cmdlets and you can replace your script with one line:

Get-QADUser -SizeLimit 0 | select name,postofficebox | Export-CSV c:\users.csv -NoTypeInformation

Open in new window


JJ
0
 
LVL 6

Assisted Solution

by:rwskas
rwskas earned 1000 total points
ID: 38400013
To maintain using your same script, use Out-String on the property lines.

So something like...

$myUSer = "" | Select-Object Name, PostOfficeBox
$myUser.Name = $ObjResult.Properties.Name | Out-String
...
etc

Will resolve your CSV issue.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

829 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